From 0745e6a0e00f5e6f000493c7e794ff53935a6664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sat, 18 Jan 2025 07:08:12 +0100 Subject: [PATCH] Fixed wrong error being reported on required initialized parameters with `isolatedDeclarations` (#60980) --- .../transformers/declarations/diagnostics.ts | 2 +- .../declarationFunctionDeclarations.d.ts | 30 +++++++++++++++++-- .../declarationFunctionDeclarations.js | 19 ++++++++++++ .../declarationFunctionDeclarations.ts | 8 +++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/compiler/transformers/declarations/diagnostics.ts b/src/compiler/transformers/declarations/diagnostics.ts index 84e952da136df..873f23cbf7987 100644 --- a/src/compiler/transformers/declarations/diagnostics.ts +++ b/src/compiler/transformers/declarations/diagnostics.ts @@ -767,7 +767,7 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver): (nod if (isSetAccessor(node.parent)) { return createAccessorTypeError(node.parent); } - const addUndefined = resolver.requiresAddingImplicitUndefined(node, /*enclosingDeclaration*/ undefined); + const addUndefined = resolver.requiresAddingImplicitUndefined(node, node.parent); if (!addUndefined && node.initializer) { return createExpressionError(node.initializer); } diff --git a/tests/baselines/reference/transpile/declarationFunctionDeclarations.d.ts b/tests/baselines/reference/transpile/declarationFunctionDeclarations.d.ts index 7814e7759fa75..af1cb8016aab5 100644 --- a/tests/baselines/reference/transpile/declarationFunctionDeclarations.d.ts +++ b/tests/baselines/reference/transpile/declarationFunctionDeclarations.d.ts @@ -45,6 +45,14 @@ export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar {} +export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + private bool: boolean, + ) {} +} //// [fnDecl.d.ts] //// type T = number[]; export declare function fnDeclBasic1(p: number[] | string[] | [T] | undefined, rParam: string): void; @@ -121,6 +129,13 @@ export declare class InClassMethodOk2 { export declare class InClassMethodBad { o(array: T | undefined, rParam: string): void; } +declare class Bar { +} +export declare class ClsWithRequiredInitializedParameter { + private arr; + private bool; + constructor(arr: Bar | undefined, bool: boolean); +} export {}; @@ -134,9 +149,10 @@ fnDecl.ts(32,45): error TS9025: Declaration emit for this parameter requires imp fnDecl.ts(37,47): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. fnDecl.ts(41,37): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. fnDecl.ts(45,35): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(51,5): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. -==== fnDecl.ts (9 errors) ==== +==== fnDecl.ts (10 errors) ==== type T = number[] export function fnDeclBasic1(p: number[] | string[] | [T] = [], rParam: string): void { }; export function fnDeclBasic2(p: (n: T) => T = () => null!, rParam: string): void { }; @@ -210,4 +226,14 @@ fnDecl.ts(45,35): error TS9025: Declaration emit for this parameter requires imp !!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. !!! related TS9028 fnDecl.ts:45:35: Add a type annotation to the parameter array. - + // https://github.com/microsoft/TypeScript/issues/60976 + class Bar {} + export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:51:5: Add a type annotation to the parameter arr. + private bool: boolean, + ) {} + } diff --git a/tests/baselines/reference/transpile/declarationFunctionDeclarations.js b/tests/baselines/reference/transpile/declarationFunctionDeclarations.js index 72ba57ff249ed..0e32cdf3471f0 100644 --- a/tests/baselines/reference/transpile/declarationFunctionDeclarations.js +++ b/tests/baselines/reference/transpile/declarationFunctionDeclarations.js @@ -45,6 +45,14 @@ export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar {} +export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + private bool: boolean, + ) {} +} //// [fnDecl.js] //// export function fnDeclBasic1(p = [], rParam) { } ; @@ -118,3 +126,14 @@ export class InClassMethodBad { o(array = [], rParam) { } } ; +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar { +} +export class ClsWithRequiredInitializedParameter { + arr; + bool; + constructor(arr = new Bar(), bool) { + this.arr = arr; + this.bool = bool; + } +} diff --git a/tests/cases/transpile/declarationFunctionDeclarations.ts b/tests/cases/transpile/declarationFunctionDeclarations.ts index 5aa0c50d4f8d9..7d9d8c82e2711 100644 --- a/tests/cases/transpile/declarationFunctionDeclarations.ts +++ b/tests/cases/transpile/declarationFunctionDeclarations.ts @@ -49,3 +49,11 @@ export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar {} +export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + private bool: boolean, + ) {} +} \ No newline at end of file