Skip to content

Commit c003609

Browse files
Andaristjakebailey
andauthored
Assume that type node annotations resolving to error types can be reused (#60195)
Co-authored-by: Jake Bailey <[email protected]>
1 parent f53d6dd commit c003609

8 files changed

+133
-14
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
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
}

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+

tests/baselines/reference/isolatedDeclarationsAddUndefined2.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export function test2(x?: Unresolved | undefined): void {}
5252
> : ^^^
5353

5454
export function test3(x?: Unresolved): void {}
55-
>test3 : (x?: any) => void
56-
> : ^ ^^^^^^^^^^^
55+
>test3 : (x?: Unresolved) => void
56+
> : ^ ^^^ ^^^^^
5757
>x : any
5858
> : ^^^
5959

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @isolatedDeclarations: true
2+
// @strict: true
3+
// @declaration: true
4+
// @moduleResolution: nodenext
5+
// @module: nodenext
6+
7+
// https://github.com/microsoft/TypeScript/issues/60192
8+
9+
import { Unresolved } from "foo";
10+
11+
export const foo1 = (type?: Unresolved): void => {};
12+
export const foo2 = (type?: Unresolved | undefined): void => {};
13+
export const foo3 = (type: Unresolved): void => {};

0 commit comments

Comments
 (0)