Skip to content

Commit 755e3e5

Browse files
committed
Add tests
1 parent cfbec96 commit 755e3e5

6 files changed

+231
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
tests/cases/compiler/intersectionPropertyCheck.ts(1,68): error TS2322: Type '{ x: string; y: number; }' is not assignable to type '{ x: string; }'.
2+
Object literal may only specify known properties, and 'y' does not exist in type '{ x: string; }'.
3+
tests/cases/compiler/intersectionPropertyCheck.ts(4,5): error TS2322: Type '{ a: { y: string; }; }' is not assignable to type '{ a?: { x?: number | undefined; } | undefined; } & { c?: string | undefined; }'.
4+
Types of property 'a' are incompatible.
5+
Type '{ y: string; }' has no properties in common with type '{ x?: number | undefined; }'.
6+
tests/cases/compiler/intersectionPropertyCheck.ts(7,3): error TS2322: Type 'T & { a: boolean; }' is not assignable to type '{ a?: string | undefined; }'.
7+
Types of property 'a' are incompatible.
8+
Type 'boolean' is not assignable to type 'string | undefined'.
9+
tests/cases/compiler/intersectionPropertyCheck.ts(17,22): error TS2322: Type 'true' is not assignable to type 'string[] | undefined'.
10+
11+
12+
==== tests/cases/compiler/intersectionPropertyCheck.ts (4 errors) ====
13+
let obj: { a: { x: string } } & { c: number } = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
14+
~~~~
15+
!!! error TS2322: Type '{ x: string; y: number; }' is not assignable to type '{ x: string; }'.
16+
!!! error TS2322: Object literal may only specify known properties, and 'y' does not exist in type '{ x: string; }'.
17+
!!! related TS6500 tests/cases/compiler/intersectionPropertyCheck.ts:1:12: The expected type comes from property 'a' which is declared here on type '{ a: { x: string; }; } & { c: number; }'
18+
19+
declare let wrong: { a: { y: string } };
20+
let weak: { a?: { x?: number } } & { c?: string } = wrong; // Nested weak object type
21+
~~~~
22+
!!! error TS2322: Type '{ a: { y: string; }; }' is not assignable to type '{ a?: { x?: number | undefined; } | undefined; } & { c?: string | undefined; }'.
23+
!!! error TS2322: Types of property 'a' are incompatible.
24+
!!! error TS2322: Type '{ y: string; }' has no properties in common with type '{ x?: number | undefined; }'.
25+
26+
function foo<T extends object>(x: { a?: string }, y: T & { a: boolean }) {
27+
x = y; // Mismatched property in source intersection
28+
~
29+
!!! error TS2322: Type 'T & { a: boolean; }' is not assignable to type '{ a?: string | undefined; }'.
30+
!!! error TS2322: Types of property 'a' are incompatible.
31+
!!! error TS2322: Type 'boolean' is not assignable to type 'string | undefined'.
32+
}
33+
34+
// Repro from #36637
35+
36+
interface Test {
37+
readonly hi?: string[]
38+
}
39+
40+
function test<T extends object>(value: T): Test {
41+
return { ...value, hi: true }
42+
~~
43+
!!! error TS2322: Type 'true' is not assignable to type 'string[] | undefined'.
44+
!!! related TS6500 tests/cases/compiler/intersectionPropertyCheck.ts:13:12: The expected type comes from property 'hi' which is declared here on type 'Test'
45+
}
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [intersectionPropertyCheck.ts]
2+
let obj: { a: { x: string } } & { c: number } = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
3+
4+
declare let wrong: { a: { y: string } };
5+
let weak: { a?: { x?: number } } & { c?: string } = wrong; // Nested weak object type
6+
7+
function foo<T extends object>(x: { a?: string }, y: T & { a: boolean }) {
8+
x = y; // Mismatched property in source intersection
9+
}
10+
11+
// Repro from #36637
12+
13+
interface Test {
14+
readonly hi?: string[]
15+
}
16+
17+
function test<T extends object>(value: T): Test {
18+
return { ...value, hi: true }
19+
}
20+
21+
22+
//// [intersectionPropertyCheck.js]
23+
"use strict";
24+
var __assign = (this && this.__assign) || function () {
25+
__assign = Object.assign || function(t) {
26+
for (var s, i = 1, n = arguments.length; i < n; i++) {
27+
s = arguments[i];
28+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
29+
t[p] = s[p];
30+
}
31+
return t;
32+
};
33+
return __assign.apply(this, arguments);
34+
};
35+
var obj = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
36+
var weak = wrong; // Nested weak object type
37+
function foo(x, y) {
38+
x = y; // Mismatched property in source intersection
39+
}
40+
function test(value) {
41+
return __assign(__assign({}, value), { hi: true });
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== tests/cases/compiler/intersectionPropertyCheck.ts ===
2+
let obj: { a: { x: string } } & { c: number } = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
3+
>obj : Symbol(obj, Decl(intersectionPropertyCheck.ts, 0, 3))
4+
>a : Symbol(a, Decl(intersectionPropertyCheck.ts, 0, 10))
5+
>x : Symbol(x, Decl(intersectionPropertyCheck.ts, 0, 15))
6+
>c : Symbol(c, Decl(intersectionPropertyCheck.ts, 0, 33))
7+
>a : Symbol(a, Decl(intersectionPropertyCheck.ts, 0, 49))
8+
>x : Symbol(x, Decl(intersectionPropertyCheck.ts, 0, 54))
9+
>y : Symbol(y, Decl(intersectionPropertyCheck.ts, 0, 66))
10+
>c : Symbol(c, Decl(intersectionPropertyCheck.ts, 0, 74))
11+
12+
declare let wrong: { a: { y: string } };
13+
>wrong : Symbol(wrong, Decl(intersectionPropertyCheck.ts, 2, 11))
14+
>a : Symbol(a, Decl(intersectionPropertyCheck.ts, 2, 20))
15+
>y : Symbol(y, Decl(intersectionPropertyCheck.ts, 2, 25))
16+
17+
let weak: { a?: { x?: number } } & { c?: string } = wrong; // Nested weak object type
18+
>weak : Symbol(weak, Decl(intersectionPropertyCheck.ts, 3, 3))
19+
>a : Symbol(a, Decl(intersectionPropertyCheck.ts, 3, 11))
20+
>x : Symbol(x, Decl(intersectionPropertyCheck.ts, 3, 17))
21+
>c : Symbol(c, Decl(intersectionPropertyCheck.ts, 3, 36))
22+
>wrong : Symbol(wrong, Decl(intersectionPropertyCheck.ts, 2, 11))
23+
24+
function foo<T extends object>(x: { a?: string }, y: T & { a: boolean }) {
25+
>foo : Symbol(foo, Decl(intersectionPropertyCheck.ts, 3, 58))
26+
>T : Symbol(T, Decl(intersectionPropertyCheck.ts, 5, 13))
27+
>x : Symbol(x, Decl(intersectionPropertyCheck.ts, 5, 31))
28+
>a : Symbol(a, Decl(intersectionPropertyCheck.ts, 5, 35))
29+
>y : Symbol(y, Decl(intersectionPropertyCheck.ts, 5, 49))
30+
>T : Symbol(T, Decl(intersectionPropertyCheck.ts, 5, 13))
31+
>a : Symbol(a, Decl(intersectionPropertyCheck.ts, 5, 58))
32+
33+
x = y; // Mismatched property in source intersection
34+
>x : Symbol(x, Decl(intersectionPropertyCheck.ts, 5, 31))
35+
>y : Symbol(y, Decl(intersectionPropertyCheck.ts, 5, 49))
36+
}
37+
38+
// Repro from #36637
39+
40+
interface Test {
41+
>Test : Symbol(Test, Decl(intersectionPropertyCheck.ts, 7, 1))
42+
43+
readonly hi?: string[]
44+
>hi : Symbol(Test.hi, Decl(intersectionPropertyCheck.ts, 11, 16))
45+
}
46+
47+
function test<T extends object>(value: T): Test {
48+
>test : Symbol(test, Decl(intersectionPropertyCheck.ts, 13, 1))
49+
>T : Symbol(T, Decl(intersectionPropertyCheck.ts, 15, 14))
50+
>value : Symbol(value, Decl(intersectionPropertyCheck.ts, 15, 32))
51+
>T : Symbol(T, Decl(intersectionPropertyCheck.ts, 15, 14))
52+
>Test : Symbol(Test, Decl(intersectionPropertyCheck.ts, 7, 1))
53+
54+
return { ...value, hi: true }
55+
>value : Symbol(value, Decl(intersectionPropertyCheck.ts, 15, 32))
56+
>hi : Symbol(hi, Decl(intersectionPropertyCheck.ts, 16, 20))
57+
}
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
=== tests/cases/compiler/intersectionPropertyCheck.ts ===
2+
let obj: { a: { x: string } } & { c: number } = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
3+
>obj : { a: { x: string; }; } & { c: number; }
4+
>a : { x: string; }
5+
>x : string
6+
>c : number
7+
>{ a: { x: 'hello', y: 2 }, c: 5 } : { a: { x: string; y: number; }; c: number; }
8+
>a : { x: string; y: number; }
9+
>{ x: 'hello', y: 2 } : { x: string; y: number; }
10+
>x : string
11+
>'hello' : "hello"
12+
>y : number
13+
>2 : 2
14+
>c : number
15+
>5 : 5
16+
17+
declare let wrong: { a: { y: string } };
18+
>wrong : { a: { y: string; }; }
19+
>a : { y: string; }
20+
>y : string
21+
22+
let weak: { a?: { x?: number } } & { c?: string } = wrong; // Nested weak object type
23+
>weak : { a?: { x?: number | undefined; } | undefined; } & { c?: string | undefined; }
24+
>a : { x?: number | undefined; } | undefined
25+
>x : number | undefined
26+
>c : string | undefined
27+
>wrong : { a: { y: string; }; }
28+
29+
function foo<T extends object>(x: { a?: string }, y: T & { a: boolean }) {
30+
>foo : <T extends object>(x: { a?: string | undefined; }, y: T & { a: boolean; }) => void
31+
>x : { a?: string | undefined; }
32+
>a : string | undefined
33+
>y : T & { a: boolean; }
34+
>a : boolean
35+
36+
x = y; // Mismatched property in source intersection
37+
>x = y : T & { a: boolean; }
38+
>x : { a?: string | undefined; }
39+
>y : T & { a: boolean; }
40+
}
41+
42+
// Repro from #36637
43+
44+
interface Test {
45+
readonly hi?: string[]
46+
>hi : string[] | undefined
47+
}
48+
49+
function test<T extends object>(value: T): Test {
50+
>test : <T extends object>(value: T) => Test
51+
>value : T
52+
53+
return { ...value, hi: true }
54+
>{ ...value, hi: true } : T & { hi: boolean; }
55+
>value : T
56+
>hi : boolean
57+
>true : true
58+
}
59+

tests/baselines/reference/weakType.errors.txt

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ tests/cases/compiler/weakType.ts(18,13): error TS2559: Type '12' has no properti
55
tests/cases/compiler/weakType.ts(19,13): error TS2559: Type '"completely wrong"' has no properties in common with type 'Settings'.
66
tests/cases/compiler/weakType.ts(20,13): error TS2559: Type 'false' has no properties in common with type 'Settings'.
77
tests/cases/compiler/weakType.ts(37,18): error TS2559: Type '{ error?: number; }' has no properties in common with type 'ChangeOptions'.
8-
tests/cases/compiler/weakType.ts(62,5): error TS2326: Types of property 'properties' are incompatible.
9-
Type '{ wrong: string; }' has no properties in common with type '{ b?: number; }'.
8+
tests/cases/compiler/weakType.ts(62,5): error TS2322: Type '{ properties: { wrong: string; }; }' is not assignable to type 'Weak & Spoiler'.
9+
Types of property 'properties' are incompatible.
10+
Type '{ wrong: string; }' has no properties in common with type '{ b?: number; }'.
1011

1112

1213
==== tests/cases/compiler/weakType.ts (8 errors) ====
@@ -90,7 +91,8 @@ tests/cases/compiler/weakType.ts(62,5): error TS2326: Types of property 'propert
9091
}
9192
let weak: Weak & Spoiler = propertiesWrong
9293
~~~~
93-
!!! error TS2326: Types of property 'properties' are incompatible.
94-
!!! error TS2326: Type '{ wrong: string; }' has no properties in common with type '{ b?: number; }'.
94+
!!! error TS2322: Type '{ properties: { wrong: string; }; }' is not assignable to type 'Weak & Spoiler'.
95+
!!! error TS2322: Types of property 'properties' are incompatible.
96+
!!! error TS2322: Type '{ wrong: string; }' has no properties in common with type '{ b?: number; }'.
9597

9698

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @strict: true
2+
3+
let obj: { a: { x: string } } & { c: number } = { a: { x: 'hello', y: 2 }, c: 5 }; // Nested excess property
4+
5+
declare let wrong: { a: { y: string } };
6+
let weak: { a?: { x?: number } } & { c?: string } = wrong; // Nested weak object type
7+
8+
function foo<T extends object>(x: { a?: string }, y: T & { a: boolean }) {
9+
x = y; // Mismatched property in source intersection
10+
}
11+
12+
// Repro from #36637
13+
14+
interface Test {
15+
readonly hi?: string[]
16+
}
17+
18+
function test<T extends object>(value: T): Test {
19+
return { ...value, hi: true }
20+
}

0 commit comments

Comments
 (0)