Avoid confusing TS9025 error in isolatedDeclarations (#60129)

This commit is contained in:
Mateusz Burzyński 2024-10-12 00:42:28 +02:00 committed by GitHub
parent a53c37d59a
commit f53d6dda5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 279 additions and 1 deletions

View File

@ -49681,7 +49681,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) {

View File

@ -0,0 +1,48 @@
isolatedDeclarationsAddUndefined2.ts(4,29): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
isolatedDeclarationsAddUndefined2.ts(8,29): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
isolatedDeclarationsAddUndefined2.ts(12,28): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
isolatedDeclarationsAddUndefined2.ts(16,28): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
isolatedDeclarationsAddUndefined2.ts(19,27): error TS2314: Generic type 'Array<T>' 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<T>' requires 1 type argument(s).
}
export class Bar2 {
constructor(private x?: Array) {}
~~~~~
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
}
export class Bar3 {
constructor(private x: Array | undefined) {}
~~~~~
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
}
export class Bar4 {
constructor(private x: Array) {}
~~~~~
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
}
export function test1(x?: Array | undefined): void {}
~~~~~
!!! error TS2314: Generic type 'Array<T>' 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'.

View File

@ -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;

View File

@ -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)

View File

@ -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?: any) => void
> : ^ ^^^^^^^^^^^
>x : any
> : ^^^

View File

@ -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 {}