Skip to content

Commit 5969ae9

Browse files
authored
fix(50075): do not strip undefined from the function class properties (#50169)
1 parent 05d7d6b commit 5969ae9

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8154,7 +8154,7 @@ namespace ts {
81548154
factory.createModifiersFromModifierFlags((isReadonlySymbol(p) ? ModifierFlags.Readonly : 0) | flag),
81558155
name,
81568156
p.flags & SymbolFlags.Optional ? factory.createToken(SyntaxKind.QuestionToken) : undefined,
8157-
isPrivate ? undefined : serializeTypeForDeclaration(context, getTypeOfSymbol(p), p, enclosingDeclaration, includePrivateSymbol, bundled),
8157+
isPrivate ? undefined : serializeTypeForDeclaration(context, getWriteTypeOfSymbol(p), p, enclosingDeclaration, includePrivateSymbol, bundled),
81588158
// TODO: https://github.com/microsoft/TypeScript/pull/32372#discussion_r328386357
81598159
// interface members can't have initializers, however class members _can_
81608160
/*initializer*/ undefined
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [a.js]
2+
/**
3+
* @param {number | undefined} x
4+
* @param {number | undefined} y
5+
*/
6+
export function Foo(x, y) {
7+
if (!(this instanceof Foo)) {
8+
return new Foo(x, y);
9+
}
10+
this.x = x;
11+
this.y = y;
12+
}
13+
14+
15+
//// [a.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports.Foo = void 0;
19+
/**
20+
* @param {number | undefined} x
21+
* @param {number | undefined} y
22+
*/
23+
function Foo(x, y) {
24+
if (!(this instanceof Foo)) {
25+
return new Foo(x, y);
26+
}
27+
this.x = x;
28+
this.y = y;
29+
}
30+
exports.Foo = Foo;
31+
32+
33+
//// [a.d.ts]
34+
/**
35+
* @param {number | undefined} x
36+
* @param {number | undefined} y
37+
*/
38+
export function Foo(x: number | undefined, y: number | undefined): Foo;
39+
export class Foo {
40+
/**
41+
* @param {number | undefined} x
42+
* @param {number | undefined} y
43+
*/
44+
constructor(x: number | undefined, y: number | undefined);
45+
x: number | undefined;
46+
y: number | undefined;
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== /a.js ===
2+
/**
3+
* @param {number | undefined} x
4+
* @param {number | undefined} y
5+
*/
6+
export function Foo(x, y) {
7+
>Foo : Symbol(Foo, Decl(a.js, 0, 0))
8+
>x : Symbol(x, Decl(a.js, 4, 20))
9+
>y : Symbol(y, Decl(a.js, 4, 22))
10+
11+
if (!(this instanceof Foo)) {
12+
>this : Symbol(Foo, Decl(a.js, 0, 0))
13+
>Foo : Symbol(Foo, Decl(a.js, 0, 0))
14+
15+
return new Foo(x, y);
16+
>Foo : Symbol(Foo, Decl(a.js, 0, 0))
17+
>x : Symbol(x, Decl(a.js, 4, 20))
18+
>y : Symbol(y, Decl(a.js, 4, 22))
19+
}
20+
this.x = x;
21+
>this.x : Symbol(Foo.x, Decl(a.js, 7, 5))
22+
>this : Symbol(Foo, Decl(a.js, 0, 0))
23+
>x : Symbol(Foo.x, Decl(a.js, 7, 5))
24+
>x : Symbol(x, Decl(a.js, 4, 20))
25+
26+
this.y = y;
27+
>this.y : Symbol(Foo.y, Decl(a.js, 8, 15))
28+
>this : Symbol(Foo, Decl(a.js, 0, 0))
29+
>y : Symbol(Foo.y, Decl(a.js, 8, 15))
30+
>y : Symbol(y, Decl(a.js, 4, 22))
31+
}
32+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== /a.js ===
2+
/**
3+
* @param {number | undefined} x
4+
* @param {number | undefined} y
5+
*/
6+
export function Foo(x, y) {
7+
>Foo : typeof Foo
8+
>x : number | undefined
9+
>y : number | undefined
10+
11+
if (!(this instanceof Foo)) {
12+
>!(this instanceof Foo) : boolean
13+
>(this instanceof Foo) : boolean
14+
>this instanceof Foo : boolean
15+
>this : this
16+
>Foo : typeof Foo
17+
18+
return new Foo(x, y);
19+
>new Foo(x, y) : Foo
20+
>Foo : typeof Foo
21+
>x : number | undefined
22+
>y : number | undefined
23+
}
24+
this.x = x;
25+
>this.x = x : number | undefined
26+
>this.x : any
27+
>this : this
28+
>x : any
29+
>x : number | undefined
30+
31+
this.y = y;
32+
>this.y = y : number | undefined
33+
>this.y : any
34+
>this : this
35+
>y : any
36+
>y : number | undefined
37+
}
38+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @strict: true
4+
// @declaration: true
5+
// @outDir: ./out
6+
// @filename: /a.js
7+
8+
/**
9+
* @param {number | undefined} x
10+
* @param {number | undefined} y
11+
*/
12+
export function Foo(x, y) {
13+
if (!(this instanceof Foo)) {
14+
return new Foo(x, y);
15+
}
16+
this.x = x;
17+
this.y = y;
18+
}

0 commit comments

Comments
 (0)