Skip to content

Commit 1d06422

Browse files
Merge pull request #598 from microsoft/main
Create a new pull request by comparing changes across two branches
2 parents aa89efc + c003609 commit 1d06422

12 files changed

+410
-13
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6240,6 +6240,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
62406240
}
62416241
}
62426242
let annotationType = getTypeFromTypeNodeWithoutContext(existing);
6243+
if (isErrorType(annotationType)) {
6244+
// allow "reusing" type nodes that resolve to error types
6245+
// those can't truly be reused but it prevents cascading errors in isolatedDeclarations
6246+
// for source with errors there is no guarantee to emit correct code anyway
6247+
return true;
6248+
}
62436249
if (requiresAddingUndefined && annotationType) {
62446250
annotationType = getOptionalType(annotationType, !isParameter(node));
62456251
}
@@ -49681,7 +49687,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4968149687
const typeNode = getNonlocalEffectiveTypeAnnotationNode(parameter);
4968249688
if (!typeNode) return false;
4968349689
const type = getTypeFromTypeNode(typeNode);
49684-
return containsUndefinedType(type);
49690+
// allow error type here to avoid confusing errors that the annotation has to contain undefined when it does in cases like this:
49691+
//
49692+
// export function fn(x?: Unresolved | undefined): void {}
49693+
return isErrorType(type) || containsUndefinedType(type);
4968549694
}
4968649695

4968749696
function requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag, enclosingDeclaration: Node | undefined) {

tests/baselines/reference/circularTypeofWithVarOrFunc.types

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ type typeAlias2 = typeof varOfAliasedType2;
2222
> : ^^^
2323

2424
function func(): typeAlias3 { return null; }
25-
>func : () => any
26-
> : ^^^^^^^^^
25+
>func : () => typeAlias3
26+
> : ^^^^^^
2727

2828
var varOfAliasedType3 = func();
2929
>varOfAliasedType3 : any
3030
> : ^^^
3131
>func() : any
3232
> : ^^^
33-
>func : () => any
34-
> : ^^^^^^^^^
33+
>func : () => typeAlias3
34+
> : ^^^^^^
3535

3636
type typeAlias3 = typeof varOfAliasedType3;
3737
>typeAlias3 : any
@@ -54,12 +54,12 @@ interface Input {
5454
type R = ReturnType<typeof mul>;
5555
>R : any
5656
> : ^^^
57-
>mul : (input: Input) => any
58-
> : ^ ^^ ^^^^^^^^
57+
>mul : (input: Input) => R
58+
> : ^ ^^ ^^^^^
5959

6060
function mul(input: Input): R {
61-
>mul : (input: Input) => any
62-
> : ^ ^^ ^^^^^^^^
61+
>mul : (input: Input) => R
62+
> : ^ ^^ ^^^^^
6363
>input : Input
6464
> : ^^^^^
6565

@@ -85,12 +85,12 @@ function mul(input: Input): R {
8585
type R2 = ReturnType<typeof f>;
8686
>R2 : any
8787
> : ^^^
88-
>f : () => any
89-
> : ^^^^^^^^^
88+
>f : () => R2
89+
> : ^^^^^^
9090

9191
function f(): R2 { return 0; }
92-
>f : () => any
93-
> : ^^^^^^^^^
92+
>f : () => R2
93+
> : ^^^^^^
9494
>0 : 0
9595
> : ^
9696

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
isolatedDeclarationErrorTypes1.ts(3,28): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
2+
3+
4+
==== isolatedDeclarationErrorTypes1.ts (1 errors) ====
5+
// https://github.com/microsoft/TypeScript/issues/60192
6+
7+
import { Unresolved } from "foo";
8+
~~~~~
9+
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
10+
11+
export const foo1 = (type?: Unresolved): void => {};
12+
export const foo2 = (type?: Unresolved | undefined): void => {};
13+
export const foo3 = (type: Unresolved): void => {};
14+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/isolatedDeclarationErrorTypes1.ts] ////
2+
3+
//// [isolatedDeclarationErrorTypes1.ts]
4+
// https://github.com/microsoft/TypeScript/issues/60192
5+
6+
import { Unresolved } from "foo";
7+
8+
export const foo1 = (type?: Unresolved): void => {};
9+
export const foo2 = (type?: Unresolved | undefined): void => {};
10+
export const foo3 = (type: Unresolved): void => {};
11+
12+
13+
//// [isolatedDeclarationErrorTypes1.js]
14+
"use strict";
15+
// https://github.com/microsoft/TypeScript/issues/60192
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
exports.foo3 = exports.foo2 = exports.foo1 = void 0;
18+
const foo1 = (type) => { };
19+
exports.foo1 = foo1;
20+
const foo2 = (type) => { };
21+
exports.foo2 = foo2;
22+
const foo3 = (type) => { };
23+
exports.foo3 = foo3;
24+
25+
26+
//// [isolatedDeclarationErrorTypes1.d.ts]
27+
import { Unresolved } from "foo";
28+
export declare const foo1: (type?: Unresolved) => void;
29+
export declare const foo2: (type?: Unresolved | undefined) => void;
30+
export declare const foo3: (type: Unresolved) => void;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/isolatedDeclarationErrorTypes1.ts] ////
2+
3+
=== isolatedDeclarationErrorTypes1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/60192
5+
6+
import { Unresolved } from "foo";
7+
>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8))
8+
9+
export const foo1 = (type?: Unresolved): void => {};
10+
>foo1 : Symbol(foo1, Decl(isolatedDeclarationErrorTypes1.ts, 4, 12))
11+
>type : Symbol(type, Decl(isolatedDeclarationErrorTypes1.ts, 4, 21))
12+
>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8))
13+
14+
export const foo2 = (type?: Unresolved | undefined): void => {};
15+
>foo2 : Symbol(foo2, Decl(isolatedDeclarationErrorTypes1.ts, 5, 12))
16+
>type : Symbol(type, Decl(isolatedDeclarationErrorTypes1.ts, 5, 21))
17+
>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8))
18+
19+
export const foo3 = (type: Unresolved): void => {};
20+
>foo3 : Symbol(foo3, Decl(isolatedDeclarationErrorTypes1.ts, 6, 12))
21+
>type : Symbol(type, Decl(isolatedDeclarationErrorTypes1.ts, 6, 21))
22+
>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8))
23+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/isolatedDeclarationErrorTypes1.ts] ////
2+
3+
=== isolatedDeclarationErrorTypes1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/60192
5+
6+
import { Unresolved } from "foo";
7+
>Unresolved : any
8+
> : ^^^
9+
10+
export const foo1 = (type?: Unresolved): void => {};
11+
>foo1 : (type?: Unresolved) => void
12+
> : ^ ^^^ ^^^^^
13+
>(type?: Unresolved): void => {} : (type?: Unresolved) => void
14+
> : ^ ^^^ ^^^^^
15+
>type : any
16+
> : ^^^
17+
18+
export const foo2 = (type?: Unresolved | undefined): void => {};
19+
>foo2 : (type?: Unresolved | undefined) => void
20+
> : ^ ^^^ ^^^^^
21+
>(type?: Unresolved | undefined): void => {} : (type?: Unresolved | undefined) => void
22+
> : ^ ^^^ ^^^^^
23+
>type : any
24+
> : ^^^
25+
26+
export const foo3 = (type: Unresolved): void => {};
27+
>foo3 : (type: Unresolved) => void
28+
> : ^ ^^ ^^^^^
29+
>(type: Unresolved): void => {} : (type: Unresolved) => void
30+
> : ^ ^^ ^^^^^
31+
>type : Unresolved
32+
> : ^^^^^^^^^^
33+
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+

0 commit comments

Comments
 (0)