Skip to content

Commit 3e25424

Browse files
authored
fix(43408): emit nullable/optional types on getters (#43476)
1 parent b31b0eb commit 3e25424

File tree

5 files changed

+747
-1
lines changed

5 files changed

+747
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6004,7 +6004,7 @@ namespace ts {
60046004
function serializeTypeForDeclaration(context: NodeBuilderContext, type: Type, symbol: Symbol, enclosingDeclaration: Node | undefined, includePrivateSymbol?: (s: Symbol) => void, bundled?: boolean) {
60056005
if (type !== errorType && enclosingDeclaration) {
60066006
const declWithExistingAnnotation = getDeclarationWithTypeAnnotation(symbol, enclosingDeclaration);
6007-
if (declWithExistingAnnotation && !isFunctionLikeDeclaration(declWithExistingAnnotation)) {
6007+
if (declWithExistingAnnotation && !isFunctionLikeDeclaration(declWithExistingAnnotation) && !isGetAccessorDeclaration(declWithExistingAnnotation)) {
60086008
// try to reuse the existing annotation
60096009
const existing = getEffectiveTypeAnnotationNode(declWithExistingAnnotation)!;
60106010
if (getTypeFromTypeNode(existing) === type && existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing, type)) {
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
//// [index.js]
2+
class С1 {
3+
/** @type {string=} */
4+
p1 = undefined;
5+
6+
/** @type {string | undefined} */
7+
p2 = undefined;
8+
9+
/** @type {?string} */
10+
p3 = null;
11+
12+
/** @type {string | null} */
13+
p4 = null;
14+
}
15+
16+
class С2 {
17+
/** @type {string=} */
18+
get p1() {
19+
return undefined;
20+
}
21+
22+
/** @type {string | undefined} */
23+
get p2() {
24+
return undefined;
25+
}
26+
27+
/** @type {?string} */
28+
get p3() {
29+
return null;
30+
}
31+
32+
/** @type {string | null} */
33+
get p4() {
34+
return null;
35+
}
36+
}
37+
38+
39+
class С3 {
40+
/** @type {string=} */
41+
get p1() {
42+
return undefined;
43+
}
44+
45+
/** @param {string=} value */
46+
set p1(value) {
47+
this.p1 = value;
48+
}
49+
50+
/** @type {string | undefined} */
51+
get p2() {
52+
return undefined;
53+
}
54+
55+
/** @param {string | undefined} value */
56+
set p2(value) {
57+
this.p2 = value;
58+
}
59+
60+
/** @type {?string} */
61+
get p3() {
62+
return null;
63+
}
64+
65+
/** @param {?string} value */
66+
set p3(value) {
67+
this.p3 = value;
68+
}
69+
70+
/** @type {string | null} */
71+
get p4() {
72+
return null;
73+
}
74+
75+
/** @param {string | null} value */
76+
set p4(value) {
77+
this.p4 = value;
78+
}
79+
}
80+
81+
82+
class С4 {
83+
/** @param {string=} value */
84+
set p1(value) {
85+
this.p1 = value;
86+
}
87+
88+
/** @param {string | undefined} value */
89+
set p2(value) {
90+
this.p2 = value;
91+
}
92+
93+
/** @param {?string} value */
94+
set p3(value) {
95+
this.p3 = value;
96+
}
97+
98+
/** @param {string | null} value */
99+
set p4(value) {
100+
this.p4 = value;
101+
}
102+
}
103+
104+
105+
//// [index.js]
106+
"use strict";
107+
class С1 {
108+
/** @type {string=} */
109+
p1 = undefined;
110+
/** @type {string | undefined} */
111+
p2 = undefined;
112+
/** @type {?string} */
113+
p3 = null;
114+
/** @type {string | null} */
115+
p4 = null;
116+
}
117+
class С2 {
118+
/** @type {string=} */
119+
get p1() {
120+
return undefined;
121+
}
122+
/** @type {string | undefined} */
123+
get p2() {
124+
return undefined;
125+
}
126+
/** @type {?string} */
127+
get p3() {
128+
return null;
129+
}
130+
/** @type {string | null} */
131+
get p4() {
132+
return null;
133+
}
134+
}
135+
class С3 {
136+
/** @type {string=} */
137+
get p1() {
138+
return undefined;
139+
}
140+
/** @param {string=} value */
141+
set p1(value) {
142+
this.p1 = value;
143+
}
144+
/** @type {string | undefined} */
145+
get p2() {
146+
return undefined;
147+
}
148+
/** @param {string | undefined} value */
149+
set p2(value) {
150+
this.p2 = value;
151+
}
152+
/** @type {?string} */
153+
get p3() {
154+
return null;
155+
}
156+
/** @param {?string} value */
157+
set p3(value) {
158+
this.p3 = value;
159+
}
160+
/** @type {string | null} */
161+
get p4() {
162+
return null;
163+
}
164+
/** @param {string | null} value */
165+
set p4(value) {
166+
this.p4 = value;
167+
}
168+
}
169+
class С4 {
170+
/** @param {string=} value */
171+
set p1(value) {
172+
this.p1 = value;
173+
}
174+
/** @param {string | undefined} value */
175+
set p2(value) {
176+
this.p2 = value;
177+
}
178+
/** @param {?string} value */
179+
set p3(value) {
180+
this.p3 = value;
181+
}
182+
/** @param {string | null} value */
183+
set p4(value) {
184+
this.p4 = value;
185+
}
186+
}
187+
188+
189+
//// [index.d.ts]
190+
declare class С1 {
191+
/** @type {string=} */
192+
p1: string | undefined;
193+
/** @type {string | undefined} */
194+
p2: string | undefined;
195+
/** @type {?string} */
196+
p3: string | null;
197+
/** @type {string | null} */
198+
p4: string | null;
199+
}
200+
declare class С2 {
201+
/** @type {string=} */
202+
get p1(): string | undefined;
203+
/** @type {string | undefined} */
204+
get p2(): string | undefined;
205+
/** @type {?string} */
206+
get p3(): string | null;
207+
/** @type {string | null} */
208+
get p4(): string | null;
209+
}
210+
declare class С3 {
211+
/** @param {string=} value */
212+
set p1(arg: string | undefined);
213+
/** @type {string=} */
214+
get p1(): string | undefined;
215+
/** @param {string | undefined} value */
216+
set p2(arg: string | undefined);
217+
/** @type {string | undefined} */
218+
get p2(): string | undefined;
219+
/** @param {?string} value */
220+
set p3(arg: string | null);
221+
/** @type {?string} */
222+
get p3(): string | null;
223+
/** @param {string | null} value */
224+
set p4(arg: string | null);
225+
/** @type {string | null} */
226+
get p4(): string | null;
227+
}
228+
declare class С4 {
229+
/** @param {string=} value */
230+
set p1(arg: string | undefined);
231+
/** @param {string | undefined} value */
232+
set p2(arg: string | undefined);
233+
/** @param {?string} value */
234+
set p3(arg: string | null);
235+
/** @param {string | null} value */
236+
set p4(arg: string | null);
237+
}

0 commit comments

Comments
 (0)