Skip to content

Commit

Permalink
Fixed wrong error being reported on required initialized parameters w…
Browse files Browse the repository at this point in the history
…ith `isolatedDeclarations` (#60980)
  • Loading branch information
Andarist authored Jan 18, 2025
1 parent a086a3c commit 0745e6a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/compiler/transformers/declarations/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {};


Expand All @@ -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 { };
Expand Down Expand Up @@ -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,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
;
Expand Down Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions tests/cases/transpile/declarationFunctionDeclarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
}

0 comments on commit 0745e6a

Please sign in to comment.