Skip to content

Commit 5c5d374

Browse files
authored
Merge pull request #42904 from a-tarasyuk/fix/42339
fix(42339): "import" missing in transpiled code when variable typed as `unknown`
2 parents 0791bb0 + 74e3ad9 commit 5c5d374

File tree

6 files changed

+176
-8
lines changed

6 files changed

+176
-8
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25475,15 +25475,20 @@ namespace ts {
2547525475
hasComputedNumberProperty = false;
2547625476
}
2547725477
const type = getReducedType(checkExpression(memberDecl.expression));
25478-
if (!isValidSpreadType(type)) {
25479-
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
25480-
return errorType;
25478+
if (isValidSpreadType(type)) {
25479+
if (allPropertiesTable) {
25480+
checkSpreadPropOverrides(type, allPropertiesTable, memberDecl);
25481+
}
25482+
offset = propertiesArray.length;
25483+
if (spread === errorType) {
25484+
continue;
25485+
}
25486+
spread = getSpreadType(spread, type, node.symbol, objectFlags, inConstContext);
2548125487
}
25482-
if (allPropertiesTable) {
25483-
checkSpreadPropOverrides(type, allPropertiesTable, memberDecl);
25488+
else {
25489+
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
25490+
spread = errorType;
2548425491
}
25485-
spread = getSpreadType(spread, type, node.symbol, objectFlags, inConstContext);
25486-
offset = propertiesArray.length;
2548725492
continue;
2548825493
}
2548925494
else {
@@ -25532,6 +25537,10 @@ namespace ts {
2553225537
}
2553325538
}
2553425539

25540+
if (spread === errorType) {
25541+
return errorType;
25542+
}
25543+
2553525544
if (spread !== emptyObjectType) {
2553625545
if (propertiesArray.length > 0) {
2553725546
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, objectFlags, inConstContext);

tests/baselines/reference/spreadUnion3.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ tests/cases/conformance/types/spread/spreadUnion3.ts(2,14): error TS2322: Type '
22
tests/cases/conformance/types/spread/spreadUnion3.ts(9,9): error TS2322: Type 'number | undefined' is not assignable to type 'number'.
33
Type 'undefined' is not assignable to type 'number'.
44
tests/cases/conformance/types/spread/spreadUnion3.ts(17,11): error TS2698: Spread types may only be created from object types.
5+
tests/cases/conformance/types/spread/spreadUnion3.ts(17,37): error TS2698: Spread types may only be created from object types.
56
tests/cases/conformance/types/spread/spreadUnion3.ts(18,11): error TS2698: Spread types may only be created from object types.
67

78

8-
==== tests/cases/conformance/types/spread/spreadUnion3.ts (4 errors) ====
9+
==== tests/cases/conformance/types/spread/spreadUnion3.ts (5 errors) ====
910
function f(x: { y: string } | undefined): { y: string } {
1011
return { y: 123, ...x } // y: string | number
1112
~
@@ -30,6 +31,8 @@ tests/cases/conformance/types/spread/spreadUnion3.ts(18,11): error TS2698: Sprea
3031
declare const nullAndUndefinedUnion: null | undefined;
3132
var x = { ...nullAndUndefinedUnion, ...nullAndUndefinedUnion };
3233
~~~~~~~~~~~~~~~~~~~~~~~~
34+
!!! error TS2698: Spread types may only be created from object types.
35+
~~~~~~~~~~~~~~~~~~~~~~~~
3336
!!! error TS2698: Spread types may only be created from object types.
3437
var y = { ...nullAndUndefinedUnion };
3538
~~~~~~~~~~~~~~~~~~~~~~~~
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/unusedImportWithSpread.ts] ////
2+
3+
//// [a.ts]
4+
export default { a: 1 };
5+
6+
//// [b1.ts]
7+
import a from "./a";
8+
9+
const b1 = {} as unknown;
10+
({
11+
// @ts-ignore
12+
...b1,
13+
a
14+
})
15+
16+
//// [b2.ts]
17+
import a from "./a";
18+
19+
const b2 = {} as never;
20+
({
21+
// @ts-ignore
22+
...b2,
23+
a
24+
})
25+
26+
27+
//// [a.js]
28+
export default { a: 1 };
29+
//// [b1.js]
30+
import a from "./a";
31+
const b1 = {};
32+
({
33+
// @ts-ignore
34+
...b1,
35+
a
36+
});
37+
//// [b2.js]
38+
import a from "./a";
39+
const b2 = {};
40+
({
41+
// @ts-ignore
42+
...b2,
43+
a
44+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default { a: 1 };
3+
>a : Symbol(a, Decl(a.ts, 0, 16))
4+
5+
=== tests/cases/compiler/b1.ts ===
6+
import a from "./a";
7+
>a : Symbol(a, Decl(b1.ts, 0, 6))
8+
9+
const b1 = {} as unknown;
10+
>b1 : Symbol(b1, Decl(b1.ts, 2, 5))
11+
12+
({
13+
// @ts-ignore
14+
...b1,
15+
>b1 : Symbol(b1, Decl(b1.ts, 2, 5))
16+
17+
a
18+
>a : Symbol(a, Decl(b1.ts, 5, 10))
19+
20+
})
21+
22+
=== tests/cases/compiler/b2.ts ===
23+
import a from "./a";
24+
>a : Symbol(a, Decl(b2.ts, 0, 6))
25+
26+
const b2 = {} as never;
27+
>b2 : Symbol(b2, Decl(b2.ts, 2, 5))
28+
29+
({
30+
// @ts-ignore
31+
...b2,
32+
>b2 : Symbol(b2, Decl(b2.ts, 2, 5))
33+
34+
a
35+
>a : Symbol(a, Decl(b2.ts, 5, 10))
36+
37+
})
38+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default { a: 1 };
3+
>{ a: 1 } : { a: number; }
4+
>a : number
5+
>1 : 1
6+
7+
=== tests/cases/compiler/b1.ts ===
8+
import a from "./a";
9+
>a : { a: number; }
10+
11+
const b1 = {} as unknown;
12+
>b1 : unknown
13+
>{} as unknown : unknown
14+
>{} : {}
15+
16+
({
17+
>({ // @ts-ignore ...b1, a}) : error
18+
>{ // @ts-ignore ...b1, a} : error
19+
20+
// @ts-ignore
21+
...b1,
22+
>b1 : unknown
23+
24+
a
25+
>a : { a: number; }
26+
27+
})
28+
29+
=== tests/cases/compiler/b2.ts ===
30+
import a from "./a";
31+
>a : { a: number; }
32+
33+
const b2 = {} as never;
34+
>b2 : never
35+
>{} as never : never
36+
>{} : {}
37+
38+
({
39+
>({ // @ts-ignore ...b2, a}) : error
40+
>{ // @ts-ignore ...b2, a} : error
41+
42+
// @ts-ignore
43+
...b2,
44+
>b2 : never
45+
46+
a
47+
>a : { a: number; }
48+
49+
})
50+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @target: esnext
2+
3+
// @filename: a.ts
4+
export default { a: 1 };
5+
6+
// @filename: b1.ts
7+
import a from "./a";
8+
9+
const b1 = {} as unknown;
10+
({
11+
// @ts-ignore
12+
...b1,
13+
a
14+
})
15+
16+
// @filename: b2.ts
17+
import a from "./a";
18+
19+
const b2 = {} as never;
20+
({
21+
// @ts-ignore
22+
...b2,
23+
a
24+
})

0 commit comments

Comments
 (0)