Skip to content

Commit 0745e6a

Browse files
authored
Fixed wrong error being reported on required initialized parameters with isolatedDeclarations (#60980)
1 parent a086a3c commit 0745e6a

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/compiler/transformers/declarations/diagnostics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver): (nod
767767
if (isSetAccessor(node.parent)) {
768768
return createAccessorTypeError(node.parent);
769769
}
770-
const addUndefined = resolver.requiresAddingImplicitUndefined(node, /*enclosingDeclaration*/ undefined);
770+
const addUndefined = resolver.requiresAddingImplicitUndefined(node, node.parent);
771771
if (!addUndefined && node.initializer) {
772772
return createExpressionError(node.initializer);
773773
}

tests/baselines/reference/transpile/declarationFunctionDeclarations.d.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void {
4545
export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } };
4646
export class InClassMethodBad { o(array: T = [], rParam: string): void { } };
4747

48+
// https://github.com/microsoft/TypeScript/issues/60976
49+
class Bar {}
50+
export class ClsWithRequiredInitializedParameter {
51+
constructor(
52+
private arr: Bar = new Bar(),
53+
private bool: boolean,
54+
) {}
55+
}
4856
//// [fnDecl.d.ts] ////
4957
type T = number[];
5058
export declare function fnDeclBasic1(p: number[] | string[] | [T] | undefined, rParam: string): void;
@@ -121,6 +129,13 @@ export declare class InClassMethodOk2 {
121129
export declare class InClassMethodBad {
122130
o(array: T | undefined, rParam: string): void;
123131
}
132+
declare class Bar {
133+
}
134+
export declare class ClsWithRequiredInitializedParameter {
135+
private arr;
136+
private bool;
137+
constructor(arr: Bar | undefined, bool: boolean);
138+
}
124139
export {};
125140

126141

@@ -134,9 +149,10 @@ fnDecl.ts(32,45): error TS9025: Declaration emit for this parameter requires imp
134149
fnDecl.ts(37,47): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations.
135150
fnDecl.ts(41,37): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations.
136151
fnDecl.ts(45,35): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations.
152+
fnDecl.ts(51,5): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations.
137153

138154

139-
==== fnDecl.ts (9 errors) ====
155+
==== fnDecl.ts (10 errors) ====
140156
type T = number[]
141157
export function fnDeclBasic1(p: number[] | string[] | [T] = [], rParam: string): void { };
142158
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
210226
!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations.
211227
!!! related TS9028 fnDecl.ts:45:35: Add a type annotation to the parameter array.
212228

213-
229+
// https://github.com/microsoft/TypeScript/issues/60976
230+
class Bar {}
231+
export class ClsWithRequiredInitializedParameter {
232+
constructor(
233+
private arr: Bar = new Bar(),
234+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235+
!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations.
236+
!!! related TS9028 fnDecl.ts:51:5: Add a type annotation to the parameter arr.
237+
private bool: boolean,
238+
) {}
239+
}

tests/baselines/reference/transpile/declarationFunctionDeclarations.js

+19
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void {
4545
export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } };
4646
export class InClassMethodBad { o(array: T = [], rParam: string): void { } };
4747

48+
// https://github.com/microsoft/TypeScript/issues/60976
49+
class Bar {}
50+
export class ClsWithRequiredInitializedParameter {
51+
constructor(
52+
private arr: Bar = new Bar(),
53+
private bool: boolean,
54+
) {}
55+
}
4856
//// [fnDecl.js] ////
4957
export function fnDeclBasic1(p = [], rParam) { }
5058
;
@@ -118,3 +126,14 @@ export class InClassMethodBad {
118126
o(array = [], rParam) { }
119127
}
120128
;
129+
// https://github.com/microsoft/TypeScript/issues/60976
130+
class Bar {
131+
}
132+
export class ClsWithRequiredInitializedParameter {
133+
arr;
134+
bool;
135+
constructor(arr = new Bar(), bool) {
136+
this.arr = arr;
137+
this.bool = bool;
138+
}
139+
}

tests/cases/transpile/declarationFunctionDeclarations.ts

+8
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void {
4949
export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } };
5050
export class InClassMethodBad { o(array: T = [], rParam: string): void { } };
5151

52+
// https://github.com/microsoft/TypeScript/issues/60976
53+
class Bar {}
54+
export class ClsWithRequiredInitializedParameter {
55+
constructor(
56+
private arr: Bar = new Bar(),
57+
private bool: boolean,
58+
) {}
59+
}

0 commit comments

Comments
 (0)