Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Mar 3, 2020
1 parent adeb532 commit f902f8a
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
tests/cases/compiler/intersectionsAndOptionalProperties.ts(5,1): error TS2322: Type '{ a: null; b: string; }' is not assignable to type '{ a?: number | undefined; b: string; }'.
Types of property 'a' are incompatible.
Type 'null' is not assignable to type 'number | undefined'.
tests/cases/compiler/intersectionsAndOptionalProperties.ts(6,1): error TS2322: Type '{ a: null; } & { b: string; }' is not assignable to type '{ a?: number | undefined; b: string; }'.
Types of property 'a' are incompatible.
Type 'null' is not assignable to type 'number | undefined'.
tests/cases/compiler/intersectionsAndOptionalProperties.ts(19,5): error TS2322: Type 'From' is not assignable to type 'To'.
Types of property 'field' are incompatible.
Type 'null' is not assignable to type 'number | undefined'.
tests/cases/compiler/intersectionsAndOptionalProperties.ts(20,5): error TS2322: Type 'null' is not assignable to type 'number | undefined'.


==== tests/cases/compiler/intersectionsAndOptionalProperties.ts (4 errors) ====
declare let x: { a?: number, b: string };
declare let y: { a: null, b: string };
declare let z: { a: null } & { b: string };

x = y; // Error
~
!!! error TS2322: Type '{ a: null; b: string; }' is not assignable to type '{ a?: number | undefined; b: string; }'.
!!! error TS2322: Types of property 'a' are incompatible.
!!! error TS2322: Type 'null' is not assignable to type 'number | undefined'.
x = z; // Error
~
!!! error TS2322: Type '{ a: null; } & { b: string; }' is not assignable to type '{ a?: number | undefined; b: string; }'.
!!! error TS2322: Types of property 'a' are incompatible.
!!! error TS2322: Type 'null' is not assignable to type 'number | undefined'.

// Repro from #36604

interface To {
field?: number;
anotherField: string;
}

type From = { field: null } & Omit<To, 'field'>;

function foo(v: From) {
let x: To;
x = v; // Error
~
!!! error TS2322: Type 'From' is not assignable to type 'To'.
!!! error TS2322: Types of property 'field' are incompatible.
!!! error TS2322: Type 'null' is not assignable to type 'number | undefined'.
x.field = v.field; // Error
~~~~~~~
!!! error TS2322: Type 'null' is not assignable to type 'number | undefined'.
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/intersectionsAndOptionalProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [intersectionsAndOptionalProperties.ts]
declare let x: { a?: number, b: string };
declare let y: { a: null, b: string };
declare let z: { a: null } & { b: string };

x = y; // Error
x = z; // Error

// Repro from #36604

interface To {
field?: number;
anotherField: string;
}

type From = { field: null } & Omit<To, 'field'>;

function foo(v: From) {
let x: To;
x = v; // Error
x.field = v.field; // Error
}


//// [intersectionsAndOptionalProperties.js]
"use strict";
x = y; // Error
x = z; // Error
function foo(v) {
var x;
x = v; // Error
x.field = v.field; // Error
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
=== tests/cases/compiler/intersectionsAndOptionalProperties.ts ===
declare let x: { a?: number, b: string };
>x : Symbol(x, Decl(intersectionsAndOptionalProperties.ts, 0, 11))
>a : Symbol(a, Decl(intersectionsAndOptionalProperties.ts, 0, 16))
>b : Symbol(b, Decl(intersectionsAndOptionalProperties.ts, 0, 28))

declare let y: { a: null, b: string };
>y : Symbol(y, Decl(intersectionsAndOptionalProperties.ts, 1, 11))
>a : Symbol(a, Decl(intersectionsAndOptionalProperties.ts, 1, 16))
>b : Symbol(b, Decl(intersectionsAndOptionalProperties.ts, 1, 25))

declare let z: { a: null } & { b: string };
>z : Symbol(z, Decl(intersectionsAndOptionalProperties.ts, 2, 11))
>a : Symbol(a, Decl(intersectionsAndOptionalProperties.ts, 2, 16))
>b : Symbol(b, Decl(intersectionsAndOptionalProperties.ts, 2, 30))

x = y; // Error
>x : Symbol(x, Decl(intersectionsAndOptionalProperties.ts, 0, 11))
>y : Symbol(y, Decl(intersectionsAndOptionalProperties.ts, 1, 11))

x = z; // Error
>x : Symbol(x, Decl(intersectionsAndOptionalProperties.ts, 0, 11))
>z : Symbol(z, Decl(intersectionsAndOptionalProperties.ts, 2, 11))

// Repro from #36604

interface To {
>To : Symbol(To, Decl(intersectionsAndOptionalProperties.ts, 5, 6))

field?: number;
>field : Symbol(To.field, Decl(intersectionsAndOptionalProperties.ts, 9, 14))

anotherField: string;
>anotherField : Symbol(To.anotherField, Decl(intersectionsAndOptionalProperties.ts, 10, 19))
}

type From = { field: null } & Omit<To, 'field'>;
>From : Symbol(From, Decl(intersectionsAndOptionalProperties.ts, 12, 1))
>field : Symbol(field, Decl(intersectionsAndOptionalProperties.ts, 14, 14))
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
>To : Symbol(To, Decl(intersectionsAndOptionalProperties.ts, 5, 6))

function foo(v: From) {
>foo : Symbol(foo, Decl(intersectionsAndOptionalProperties.ts, 14, 49))
>v : Symbol(v, Decl(intersectionsAndOptionalProperties.ts, 16, 13))
>From : Symbol(From, Decl(intersectionsAndOptionalProperties.ts, 12, 1))

let x: To;
>x : Symbol(x, Decl(intersectionsAndOptionalProperties.ts, 17, 7))
>To : Symbol(To, Decl(intersectionsAndOptionalProperties.ts, 5, 6))

x = v; // Error
>x : Symbol(x, Decl(intersectionsAndOptionalProperties.ts, 17, 7))
>v : Symbol(v, Decl(intersectionsAndOptionalProperties.ts, 16, 13))

x.field = v.field; // Error
>x.field : Symbol(To.field, Decl(intersectionsAndOptionalProperties.ts, 9, 14))
>x : Symbol(x, Decl(intersectionsAndOptionalProperties.ts, 17, 7))
>field : Symbol(To.field, Decl(intersectionsAndOptionalProperties.ts, 9, 14))
>v.field : Symbol(field, Decl(intersectionsAndOptionalProperties.ts, 14, 14))
>v : Symbol(v, Decl(intersectionsAndOptionalProperties.ts, 16, 13))
>field : Symbol(field, Decl(intersectionsAndOptionalProperties.ts, 14, 14))
}

65 changes: 65 additions & 0 deletions tests/baselines/reference/intersectionsAndOptionalProperties.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
=== tests/cases/compiler/intersectionsAndOptionalProperties.ts ===
declare let x: { a?: number, b: string };
>x : { a?: number | undefined; b: string; }
>a : number | undefined
>b : string

declare let y: { a: null, b: string };
>y : { a: null; b: string; }
>a : null
>null : null
>b : string

declare let z: { a: null } & { b: string };
>z : { a: null; } & { b: string; }
>a : null
>null : null
>b : string

x = y; // Error
>x = y : { a: null; b: string; }
>x : { a?: number | undefined; b: string; }
>y : { a: null; b: string; }

x = z; // Error
>x = z : { a: null; } & { b: string; }
>x : { a?: number | undefined; b: string; }
>z : { a: null; } & { b: string; }

// Repro from #36604

interface To {
field?: number;
>field : number | undefined

anotherField: string;
>anotherField : string
}

type From = { field: null } & Omit<To, 'field'>;
>From : From
>field : null
>null : null

function foo(v: From) {
>foo : (v: From) => void
>v : From

let x: To;
>x : To

x = v; // Error
>x = v : From
>x : To
>v : From

x.field = v.field; // Error
>x.field = v.field : null
>x.field : number | undefined
>x : To
>field : number | undefined
>v.field : null
>v : From
>field : null
}

23 changes: 23 additions & 0 deletions tests/cases/compiler/intersectionsAndOptionalProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @strict: true

declare let x: { a?: number, b: string };
declare let y: { a: null, b: string };
declare let z: { a: null } & { b: string };

x = y; // Error
x = z; // Error

// Repro from #36604

interface To {
field?: number;
anotherField: string;
}

type From = { field: null } & Omit<To, 'field'>;

function foo(v: From) {
let x: To;
x = v; // Error
x.field = v.field; // Error
}

0 comments on commit f902f8a

Please sign in to comment.