diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1f3882dff7269..91bbf4d29d3f0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6240,6 +6240,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } } let annotationType = getTypeFromTypeNodeWithoutContext(existing); + if (isErrorType(annotationType)) { + // allow "reusing" type nodes that resolve to error types + // those can't truly be reused but it prevents cascading errors in isolatedDeclarations + // for source with errors there is no guarantee to emit correct code anyway + return true; + } if (requiresAddingUndefined && annotationType) { annotationType = getOptionalType(annotationType, !isParameter(node)); } @@ -49681,7 +49687,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const typeNode = getNonlocalEffectiveTypeAnnotationNode(parameter); if (!typeNode) return false; const type = getTypeFromTypeNode(typeNode); - return containsUndefinedType(type); + // allow error type here to avoid confusing errors that the annotation has to contain undefined when it does in cases like this: + // + // export function fn(x?: Unresolved | undefined): void {} + return isErrorType(type) || containsUndefinedType(type); } function requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag, enclosingDeclaration: Node | undefined) { diff --git a/tests/baselines/reference/circularTypeofWithVarOrFunc.types b/tests/baselines/reference/circularTypeofWithVarOrFunc.types index 823cd27cbd554..4c23e013d7626 100644 --- a/tests/baselines/reference/circularTypeofWithVarOrFunc.types +++ b/tests/baselines/reference/circularTypeofWithVarOrFunc.types @@ -22,16 +22,16 @@ type typeAlias2 = typeof varOfAliasedType2; > : ^^^ function func(): typeAlias3 { return null; } ->func : () => any -> : ^^^^^^^^^ +>func : () => typeAlias3 +> : ^^^^^^ var varOfAliasedType3 = func(); >varOfAliasedType3 : any > : ^^^ >func() : any > : ^^^ ->func : () => any -> : ^^^^^^^^^ +>func : () => typeAlias3 +> : ^^^^^^ type typeAlias3 = typeof varOfAliasedType3; >typeAlias3 : any @@ -54,12 +54,12 @@ interface Input { type R = ReturnType; >R : any > : ^^^ ->mul : (input: Input) => any -> : ^ ^^ ^^^^^^^^ +>mul : (input: Input) => R +> : ^ ^^ ^^^^^ function mul(input: Input): R { ->mul : (input: Input) => any -> : ^ ^^ ^^^^^^^^ +>mul : (input: Input) => R +> : ^ ^^ ^^^^^ >input : Input > : ^^^^^ @@ -85,12 +85,12 @@ function mul(input: Input): R { type R2 = ReturnType; >R2 : any > : ^^^ ->f : () => any -> : ^^^^^^^^^ +>f : () => R2 +> : ^^^^^^ function f(): R2 { return 0; } ->f : () => any -> : ^^^^^^^^^ +>f : () => R2 +> : ^^^^^^ >0 : 0 > : ^ diff --git a/tests/baselines/reference/isolatedDeclarationErrorTypes1.errors.txt b/tests/baselines/reference/isolatedDeclarationErrorTypes1.errors.txt new file mode 100644 index 0000000000000..f89ec2f8264de --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationErrorTypes1.errors.txt @@ -0,0 +1,14 @@ +isolatedDeclarationErrorTypes1.ts(3,28): error TS2307: Cannot find module 'foo' or its corresponding type declarations. + + +==== isolatedDeclarationErrorTypes1.ts (1 errors) ==== + // https://github.com/microsoft/TypeScript/issues/60192 + + import { Unresolved } from "foo"; + ~~~~~ +!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations. + + export const foo1 = (type?: Unresolved): void => {}; + export const foo2 = (type?: Unresolved | undefined): void => {}; + export const foo3 = (type: Unresolved): void => {}; + \ No newline at end of file diff --git a/tests/baselines/reference/isolatedDeclarationErrorTypes1.js b/tests/baselines/reference/isolatedDeclarationErrorTypes1.js new file mode 100644 index 0000000000000..1949d31f6b9dd --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationErrorTypes1.js @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/isolatedDeclarationErrorTypes1.ts] //// + +//// [isolatedDeclarationErrorTypes1.ts] +// https://github.com/microsoft/TypeScript/issues/60192 + +import { Unresolved } from "foo"; + +export const foo1 = (type?: Unresolved): void => {}; +export const foo2 = (type?: Unresolved | undefined): void => {}; +export const foo3 = (type: Unresolved): void => {}; + + +//// [isolatedDeclarationErrorTypes1.js] +"use strict"; +// https://github.com/microsoft/TypeScript/issues/60192 +Object.defineProperty(exports, "__esModule", { value: true }); +exports.foo3 = exports.foo2 = exports.foo1 = void 0; +const foo1 = (type) => { }; +exports.foo1 = foo1; +const foo2 = (type) => { }; +exports.foo2 = foo2; +const foo3 = (type) => { }; +exports.foo3 = foo3; + + +//// [isolatedDeclarationErrorTypes1.d.ts] +import { Unresolved } from "foo"; +export declare const foo1: (type?: Unresolved) => void; +export declare const foo2: (type?: Unresolved | undefined) => void; +export declare const foo3: (type: Unresolved) => void; diff --git a/tests/baselines/reference/isolatedDeclarationErrorTypes1.symbols b/tests/baselines/reference/isolatedDeclarationErrorTypes1.symbols new file mode 100644 index 0000000000000..c7da06e14380b --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationErrorTypes1.symbols @@ -0,0 +1,23 @@ +//// [tests/cases/compiler/isolatedDeclarationErrorTypes1.ts] //// + +=== isolatedDeclarationErrorTypes1.ts === +// https://github.com/microsoft/TypeScript/issues/60192 + +import { Unresolved } from "foo"; +>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8)) + +export const foo1 = (type?: Unresolved): void => {}; +>foo1 : Symbol(foo1, Decl(isolatedDeclarationErrorTypes1.ts, 4, 12)) +>type : Symbol(type, Decl(isolatedDeclarationErrorTypes1.ts, 4, 21)) +>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8)) + +export const foo2 = (type?: Unresolved | undefined): void => {}; +>foo2 : Symbol(foo2, Decl(isolatedDeclarationErrorTypes1.ts, 5, 12)) +>type : Symbol(type, Decl(isolatedDeclarationErrorTypes1.ts, 5, 21)) +>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8)) + +export const foo3 = (type: Unresolved): void => {}; +>foo3 : Symbol(foo3, Decl(isolatedDeclarationErrorTypes1.ts, 6, 12)) +>type : Symbol(type, Decl(isolatedDeclarationErrorTypes1.ts, 6, 21)) +>Unresolved : Symbol(Unresolved, Decl(isolatedDeclarationErrorTypes1.ts, 2, 8)) + diff --git a/tests/baselines/reference/isolatedDeclarationErrorTypes1.types b/tests/baselines/reference/isolatedDeclarationErrorTypes1.types new file mode 100644 index 0000000000000..2e388cf0a734a --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationErrorTypes1.types @@ -0,0 +1,33 @@ +//// [tests/cases/compiler/isolatedDeclarationErrorTypes1.ts] //// + +=== isolatedDeclarationErrorTypes1.ts === +// https://github.com/microsoft/TypeScript/issues/60192 + +import { Unresolved } from "foo"; +>Unresolved : any +> : ^^^ + +export const foo1 = (type?: Unresolved): void => {}; +>foo1 : (type?: Unresolved) => void +> : ^ ^^^ ^^^^^ +>(type?: Unresolved): void => {} : (type?: Unresolved) => void +> : ^ ^^^ ^^^^^ +>type : any +> : ^^^ + +export const foo2 = (type?: Unresolved | undefined): void => {}; +>foo2 : (type?: Unresolved | undefined) => void +> : ^ ^^^ ^^^^^ +>(type?: Unresolved | undefined): void => {} : (type?: Unresolved | undefined) => void +> : ^ ^^^ ^^^^^ +>type : any +> : ^^^ + +export const foo3 = (type: Unresolved): void => {}; +>foo3 : (type: Unresolved) => void +> : ^ ^^ ^^^^^ +>(type: Unresolved): void => {} : (type: Unresolved) => void +> : ^ ^^ ^^^^^ +>type : Unresolved +> : ^^^^^^^^^^ + diff --git a/tests/baselines/reference/isolatedDeclarationsAddUndefined2.errors.txt b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.errors.txt new file mode 100644 index 0000000000000..638ebb2a8865c --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.errors.txt @@ -0,0 +1,48 @@ +isolatedDeclarationsAddUndefined2.ts(4,29): error TS2314: Generic type 'Array' requires 1 type argument(s). +isolatedDeclarationsAddUndefined2.ts(8,29): error TS2314: Generic type 'Array' requires 1 type argument(s). +isolatedDeclarationsAddUndefined2.ts(12,28): error TS2314: Generic type 'Array' requires 1 type argument(s). +isolatedDeclarationsAddUndefined2.ts(16,28): error TS2314: Generic type 'Array' requires 1 type argument(s). +isolatedDeclarationsAddUndefined2.ts(19,27): error TS2314: Generic type 'Array' requires 1 type argument(s). +isolatedDeclarationsAddUndefined2.ts(21,27): error TS2304: Cannot find name 'Unresolved'. +isolatedDeclarationsAddUndefined2.ts(23,27): error TS2304: Cannot find name 'Unresolved'. + + +==== isolatedDeclarationsAddUndefined2.ts (7 errors) ==== + // https://github.com/microsoft/TypeScript/issues/60123 + + export class Bar { + constructor(private x?: Array | undefined) {} + ~~~~~ +!!! error TS2314: Generic type 'Array' requires 1 type argument(s). + } + + export class Bar2 { + constructor(private x?: Array) {} + ~~~~~ +!!! error TS2314: Generic type 'Array' requires 1 type argument(s). + } + + export class Bar3 { + constructor(private x: Array | undefined) {} + ~~~~~ +!!! error TS2314: Generic type 'Array' requires 1 type argument(s). + } + + export class Bar4 { + constructor(private x: Array) {} + ~~~~~ +!!! error TS2314: Generic type 'Array' requires 1 type argument(s). + } + + export function test1(x?: Array | undefined): void {} + ~~~~~ +!!! error TS2314: Generic type 'Array' requires 1 type argument(s). + + export function test2(x?: Unresolved | undefined): void {} + ~~~~~~~~~~ +!!! error TS2304: Cannot find name 'Unresolved'. + + export function test3(x?: Unresolved): void {} + ~~~~~~~~~~ +!!! error TS2304: Cannot find name 'Unresolved'. + \ No newline at end of file diff --git a/tests/baselines/reference/isolatedDeclarationsAddUndefined2.js b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.js new file mode 100644 index 0000000000000..e3f3f7ef3dbcd --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.js @@ -0,0 +1,89 @@ +//// [tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts] //// + +//// [isolatedDeclarationsAddUndefined2.ts] +// https://github.com/microsoft/TypeScript/issues/60123 + +export class Bar { + constructor(private x?: Array | undefined) {} +} + +export class Bar2 { + constructor(private x?: Array) {} +} + +export class Bar3 { + constructor(private x: Array | undefined) {} +} + +export class Bar4 { + constructor(private x: Array) {} +} + +export function test1(x?: Array | undefined): void {} + +export function test2(x?: Unresolved | undefined): void {} + +export function test3(x?: Unresolved): void {} + + +//// [isolatedDeclarationsAddUndefined2.js] +"use strict"; +// https://github.com/microsoft/TypeScript/issues/60123 +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Bar4 = exports.Bar3 = exports.Bar2 = exports.Bar = void 0; +exports.test1 = test1; +exports.test2 = test2; +exports.test3 = test3; +var Bar = /** @class */ (function () { + function Bar(x) { + this.x = x; + } + return Bar; +}()); +exports.Bar = Bar; +var Bar2 = /** @class */ (function () { + function Bar2(x) { + this.x = x; + } + return Bar2; +}()); +exports.Bar2 = Bar2; +var Bar3 = /** @class */ (function () { + function Bar3(x) { + this.x = x; + } + return Bar3; +}()); +exports.Bar3 = Bar3; +var Bar4 = /** @class */ (function () { + function Bar4(x) { + this.x = x; + } + return Bar4; +}()); +exports.Bar4 = Bar4; +function test1(x) { } +function test2(x) { } +function test3(x) { } + + +//// [isolatedDeclarationsAddUndefined2.d.ts] +export declare class Bar { + private x?; + constructor(x?: Array | undefined); +} +export declare class Bar2 { + private x?; + constructor(x?: Array); +} +export declare class Bar3 { + private x; + constructor(x: Array | undefined); +} +export declare class Bar4 { + private x; + constructor(x: Array); +} +export declare function test1(x?: Array | undefined): void; +export declare function test2(x?: Unresolved | undefined): void; +export declare function test3(x?: Unresolved): void; diff --git a/tests/baselines/reference/isolatedDeclarationsAddUndefined2.symbols b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.symbols new file mode 100644 index 0000000000000..9778b309c7495 --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.symbols @@ -0,0 +1,52 @@ +//// [tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts] //// + +=== isolatedDeclarationsAddUndefined2.ts === +// https://github.com/microsoft/TypeScript/issues/60123 + +export class Bar { +>Bar : Symbol(Bar, Decl(isolatedDeclarationsAddUndefined2.ts, 0, 0)) + + constructor(private x?: Array | undefined) {} +>x : Symbol(Bar.x, Decl(isolatedDeclarationsAddUndefined2.ts, 3, 16)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +export class Bar2 { +>Bar2 : Symbol(Bar2, Decl(isolatedDeclarationsAddUndefined2.ts, 4, 1)) + + constructor(private x?: Array) {} +>x : Symbol(Bar2.x, Decl(isolatedDeclarationsAddUndefined2.ts, 7, 16)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +export class Bar3 { +>Bar3 : Symbol(Bar3, Decl(isolatedDeclarationsAddUndefined2.ts, 8, 1)) + + constructor(private x: Array | undefined) {} +>x : Symbol(Bar3.x, Decl(isolatedDeclarationsAddUndefined2.ts, 11, 16)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +export class Bar4 { +>Bar4 : Symbol(Bar4, Decl(isolatedDeclarationsAddUndefined2.ts, 12, 1)) + + constructor(private x: Array) {} +>x : Symbol(Bar4.x, Decl(isolatedDeclarationsAddUndefined2.ts, 15, 16)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +export function test1(x?: Array | undefined): void {} +>test1 : Symbol(test1, Decl(isolatedDeclarationsAddUndefined2.ts, 16, 1)) +>x : Symbol(x, Decl(isolatedDeclarationsAddUndefined2.ts, 18, 22)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + +export function test2(x?: Unresolved | undefined): void {} +>test2 : Symbol(test2, Decl(isolatedDeclarationsAddUndefined2.ts, 18, 53)) +>x : Symbol(x, Decl(isolatedDeclarationsAddUndefined2.ts, 20, 22)) +>Unresolved : Symbol(Unresolved) + +export function test3(x?: Unresolved): void {} +>test3 : Symbol(test3, Decl(isolatedDeclarationsAddUndefined2.ts, 20, 58)) +>x : Symbol(x, Decl(isolatedDeclarationsAddUndefined2.ts, 22, 22)) +>Unresolved : Symbol(Unresolved) + diff --git a/tests/baselines/reference/isolatedDeclarationsAddUndefined2.types b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.types new file mode 100644 index 0000000000000..6ddcad9a53930 --- /dev/null +++ b/tests/baselines/reference/isolatedDeclarationsAddUndefined2.types @@ -0,0 +1,59 @@ +//// [tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts] //// + +=== isolatedDeclarationsAddUndefined2.ts === +// https://github.com/microsoft/TypeScript/issues/60123 + +export class Bar { +>Bar : Bar +> : ^^^ + + constructor(private x?: Array | undefined) {} +>x : any +> : ^^^ +} + +export class Bar2 { +>Bar2 : Bar2 +> : ^^^^ + + constructor(private x?: Array) {} +>x : any +> : ^^^ +} + +export class Bar3 { +>Bar3 : Bar3 +> : ^^^^ + + constructor(private x: Array | undefined) {} +>x : any +> : ^^^ +} + +export class Bar4 { +>Bar4 : Bar4 +> : ^^^^ + + constructor(private x: Array) {} +>x : any +> : ^^^ +} + +export function test1(x?: Array | undefined): void {} +>test1 : (x?: Array | undefined) => void +> : ^ ^^^ ^^^^^ +>x : any +> : ^^^ + +export function test2(x?: Unresolved | undefined): void {} +>test2 : (x?: Unresolved | undefined) => void +> : ^ ^^^ ^^^^^ +>x : any +> : ^^^ + +export function test3(x?: Unresolved): void {} +>test3 : (x?: Unresolved) => void +> : ^ ^^^ ^^^^^ +>x : any +> : ^^^ + diff --git a/tests/cases/compiler/isolatedDeclarationErrorTypes1.ts b/tests/cases/compiler/isolatedDeclarationErrorTypes1.ts new file mode 100644 index 0000000000000..a94ea0d47b242 --- /dev/null +++ b/tests/cases/compiler/isolatedDeclarationErrorTypes1.ts @@ -0,0 +1,13 @@ +// @isolatedDeclarations: true +// @strict: true +// @declaration: true +// @moduleResolution: nodenext +// @module: nodenext + +// https://github.com/microsoft/TypeScript/issues/60192 + +import { Unresolved } from "foo"; + +export const foo1 = (type?: Unresolved): void => {}; +export const foo2 = (type?: Unresolved | undefined): void => {}; +export const foo3 = (type: Unresolved): void => {}; diff --git a/tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts b/tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts new file mode 100644 index 0000000000000..ef28b2c542e25 --- /dev/null +++ b/tests/cases/compiler/isolatedDeclarationsAddUndefined2.ts @@ -0,0 +1,27 @@ +// @isolatedDeclarations: true +// @declaration: true +// @strict: true + +// https://github.com/microsoft/TypeScript/issues/60123 + +export class Bar { + constructor(private x?: Array | undefined) {} +} + +export class Bar2 { + constructor(private x?: Array) {} +} + +export class Bar3 { + constructor(private x: Array | undefined) {} +} + +export class Bar4 { + constructor(private x: Array) {} +} + +export function test1(x?: Array | undefined): void {} + +export function test2(x?: Unresolved | undefined): void {} + +export function test3(x?: Unresolved): void {}