Skip to content

Commit f53d6dd

Browse files
authored
Avoid confusing TS9025 error in isolatedDeclarations (microsoft#60129)
1 parent a53c37d commit f53d6dd

File tree

6 files changed

+279
-1
lines changed

6 files changed

+279
-1
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49681,7 +49681,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4968149681
const typeNode = getNonlocalEffectiveTypeAnnotationNode(parameter);
4968249682
if (!typeNode) return false;
4968349683
const type = getTypeFromTypeNode(typeNode);
49684-
return containsUndefinedType(type);
49684+
// allow error type here to avoid confusing errors that the annotation has to contain undefined when it does in cases like this:
49685+
//
49686+
// export function fn(x?: Unresolved | undefined): void {}
49687+
return isErrorType(type) || containsUndefinedType(type);
4968549688
}
4968649689

4968749690
function requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag, enclosingDeclaration: Node | undefined) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
isolatedDeclarationsAddUndefined2.ts(4,29): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
2+
isolatedDeclarationsAddUndefined2.ts(8,29): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
3+
isolatedDeclarationsAddUndefined2.ts(12,28): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
4+
isolatedDeclarationsAddUndefined2.ts(16,28): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
5+
isolatedDeclarationsAddUndefined2.ts(19,27): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
6+
isolatedDeclarationsAddUndefined2.ts(21,27): error TS2304: Cannot find name 'Unresolved'.
7+
isolatedDeclarationsAddUndefined2.ts(23,27): error TS2304: Cannot find name 'Unresolved'.
8+
9+
10+
==== isolatedDeclarationsAddUndefined2.ts (7 errors) ====
11+
// https://github.com/microsoft/TypeScript/issues/60123
12+
13+
export class Bar {
14+
constructor(private x?: Array | undefined) {}
15+
~~~~~
16+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
17+
}
18+
19+
export class Bar2 {
20+
constructor(private x?: Array) {}
21+
~~~~~
22+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
23+
}
24+
25+
export class Bar3 {
26+
constructor(private x: Array | undefined) {}
27+
~~~~~
28+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
29+
}
30+
31+
export class Bar4 {
32+
constructor(private x: Array) {}
33+
~~~~~
34+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
35+
}
36+
37+
export function test1(x?: Array | undefined): void {}
38+
~~~~~
39+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
40+
41+
export function test2(x?: Unresolved | undefined): void {}
42+
~~~~~~~~~~
43+
!!! error TS2304: Cannot find name 'Unresolved'.
44+
45+
export function test3(x?: Unresolved): void {}
46+
~~~~~~~~~~
47+
!!! error TS2304: Cannot find name 'Unresolved'.
48+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//// [tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts] ////
2+
3+
//// [isolatedDeclarationsAddUndefined2.ts]
4+
// https://github.com/microsoft/TypeScript/issues/60123
5+
6+
export class Bar {
7+
constructor(private x?: Array | undefined) {}
8+
}
9+
10+
export class Bar2 {
11+
constructor(private x?: Array) {}
12+
}
13+
14+
export class Bar3 {
15+
constructor(private x: Array | undefined) {}
16+
}
17+
18+
export class Bar4 {
19+
constructor(private x: Array) {}
20+
}
21+
22+
export function test1(x?: Array | undefined): void {}
23+
24+
export function test2(x?: Unresolved | undefined): void {}
25+
26+
export function test3(x?: Unresolved): void {}
27+
28+
29+
//// [isolatedDeclarationsAddUndefined2.js]
30+
"use strict";
31+
// https://github.com/microsoft/TypeScript/issues/60123
32+
Object.defineProperty(exports, "__esModule", { value: true });
33+
exports.Bar4 = exports.Bar3 = exports.Bar2 = exports.Bar = void 0;
34+
exports.test1 = test1;
35+
exports.test2 = test2;
36+
exports.test3 = test3;
37+
var Bar = /** @class */ (function () {
38+
function Bar(x) {
39+
this.x = x;
40+
}
41+
return Bar;
42+
}());
43+
exports.Bar = Bar;
44+
var Bar2 = /** @class */ (function () {
45+
function Bar2(x) {
46+
this.x = x;
47+
}
48+
return Bar2;
49+
}());
50+
exports.Bar2 = Bar2;
51+
var Bar3 = /** @class */ (function () {
52+
function Bar3(x) {
53+
this.x = x;
54+
}
55+
return Bar3;
56+
}());
57+
exports.Bar3 = Bar3;
58+
var Bar4 = /** @class */ (function () {
59+
function Bar4(x) {
60+
this.x = x;
61+
}
62+
return Bar4;
63+
}());
64+
exports.Bar4 = Bar4;
65+
function test1(x) { }
66+
function test2(x) { }
67+
function test3(x) { }
68+
69+
70+
//// [isolatedDeclarationsAddUndefined2.d.ts]
71+
export declare class Bar {
72+
private x?;
73+
constructor(x?: Array | undefined);
74+
}
75+
export declare class Bar2 {
76+
private x?;
77+
constructor(x?: Array);
78+
}
79+
export declare class Bar3 {
80+
private x;
81+
constructor(x: Array | undefined);
82+
}
83+
export declare class Bar4 {
84+
private x;
85+
constructor(x: Array);
86+
}
87+
export declare function test1(x?: Array | undefined): void;
88+
export declare function test2(x?: Unresolved | undefined): void;
89+
export declare function test3(x?: Unresolved): void;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts] ////
2+
3+
=== isolatedDeclarationsAddUndefined2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/60123
5+
6+
export class Bar {
7+
>Bar : Symbol(Bar, Decl(isolatedDeclarationsAddUndefined2.ts, 0, 0))
8+
9+
constructor(private x?: Array | undefined) {}
10+
>x : Symbol(Bar.x, Decl(isolatedDeclarationsAddUndefined2.ts, 3, 16))
11+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
12+
}
13+
14+
export class Bar2 {
15+
>Bar2 : Symbol(Bar2, Decl(isolatedDeclarationsAddUndefined2.ts, 4, 1))
16+
17+
constructor(private x?: Array) {}
18+
>x : Symbol(Bar2.x, Decl(isolatedDeclarationsAddUndefined2.ts, 7, 16))
19+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
20+
}
21+
22+
export class Bar3 {
23+
>Bar3 : Symbol(Bar3, Decl(isolatedDeclarationsAddUndefined2.ts, 8, 1))
24+
25+
constructor(private x: Array | undefined) {}
26+
>x : Symbol(Bar3.x, Decl(isolatedDeclarationsAddUndefined2.ts, 11, 16))
27+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
28+
}
29+
30+
export class Bar4 {
31+
>Bar4 : Symbol(Bar4, Decl(isolatedDeclarationsAddUndefined2.ts, 12, 1))
32+
33+
constructor(private x: Array) {}
34+
>x : Symbol(Bar4.x, Decl(isolatedDeclarationsAddUndefined2.ts, 15, 16))
35+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
36+
}
37+
38+
export function test1(x?: Array | undefined): void {}
39+
>test1 : Symbol(test1, Decl(isolatedDeclarationsAddUndefined2.ts, 16, 1))
40+
>x : Symbol(x, Decl(isolatedDeclarationsAddUndefined2.ts, 18, 22))
41+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
42+
43+
export function test2(x?: Unresolved | undefined): void {}
44+
>test2 : Symbol(test2, Decl(isolatedDeclarationsAddUndefined2.ts, 18, 53))
45+
>x : Symbol(x, Decl(isolatedDeclarationsAddUndefined2.ts, 20, 22))
46+
>Unresolved : Symbol(Unresolved)
47+
48+
export function test3(x?: Unresolved): void {}
49+
>test3 : Symbol(test3, Decl(isolatedDeclarationsAddUndefined2.ts, 20, 58))
50+
>x : Symbol(x, Decl(isolatedDeclarationsAddUndefined2.ts, 22, 22))
51+
>Unresolved : Symbol(Unresolved)
52+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// [tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts] ////
2+
3+
=== isolatedDeclarationsAddUndefined2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/60123
5+
6+
export class Bar {
7+
>Bar : Bar
8+
> : ^^^
9+
10+
constructor(private x?: Array | undefined) {}
11+
>x : any
12+
> : ^^^
13+
}
14+
15+
export class Bar2 {
16+
>Bar2 : Bar2
17+
> : ^^^^
18+
19+
constructor(private x?: Array) {}
20+
>x : any
21+
> : ^^^
22+
}
23+
24+
export class Bar3 {
25+
>Bar3 : Bar3
26+
> : ^^^^
27+
28+
constructor(private x: Array | undefined) {}
29+
>x : any
30+
> : ^^^
31+
}
32+
33+
export class Bar4 {
34+
>Bar4 : Bar4
35+
> : ^^^^
36+
37+
constructor(private x: Array) {}
38+
>x : any
39+
> : ^^^
40+
}
41+
42+
export function test1(x?: Array | undefined): void {}
43+
>test1 : (x?: Array | undefined) => void
44+
> : ^ ^^^ ^^^^^
45+
>x : any
46+
> : ^^^
47+
48+
export function test2(x?: Unresolved | undefined): void {}
49+
>test2 : (x?: Unresolved | undefined) => void
50+
> : ^ ^^^ ^^^^^
51+
>x : any
52+
> : ^^^
53+
54+
export function test3(x?: Unresolved): void {}
55+
>test3 : (x?: any) => void
56+
> : ^ ^^^^^^^^^^^
57+
>x : any
58+
> : ^^^
59+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @isolatedDeclarations: true
2+
// @declaration: true
3+
// @strict: true
4+
5+
// https://github.com/microsoft/TypeScript/issues/60123
6+
7+
export class Bar {
8+
constructor(private x?: Array | undefined) {}
9+
}
10+
11+
export class Bar2 {
12+
constructor(private x?: Array) {}
13+
}
14+
15+
export class Bar3 {
16+
constructor(private x: Array | undefined) {}
17+
}
18+
19+
export class Bar4 {
20+
constructor(private x: Array) {}
21+
}
22+
23+
export function test1(x?: Array | undefined): void {}
24+
25+
export function test2(x?: Unresolved | undefined): void {}
26+
27+
export function test3(x?: Unresolved): void {}

0 commit comments

Comments
 (0)