Skip to content

Commit cdbe969

Browse files
committed
Address PR comments
1 parent 960e657 commit cdbe969

File tree

7 files changed

+96
-46
lines changed

7 files changed

+96
-46
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@
17381738
"category": "Error",
17391739
"code": 2374
17401740
},
1741-
"Type '{0}' is not assignable to type '{1}' with exactOptionalPropertyTypes: true. Consider adding 'undefined' to the types of the target's properties.": {
1741+
"Type '{0}' is not assignable to type '{1}' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.": {
17421742
"category": "Error",
17431743
"code": 2375
17441744
},
@@ -1754,7 +1754,7 @@
17541754
"category": "Error",
17551755
"code": 2378
17561756
},
1757-
"Argument of type '{0}' is not assignable to parameter of type '{1}' with exactOptionalPropertyTypes: true. Consider adding 'undefined' to the types of the target's properties.": {
1757+
"Argument of type '{0}' is not assignable to parameter of type '{1}' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.": {
17581758
"category": "Error",
17591759
"code": 2379
17601760
},
@@ -1882,7 +1882,7 @@
18821882
"category": "Error",
18831883
"code": 2410
18841884
},
1885-
"Type '{0}' is not assignable to type '{1}' with exactOptionalPropertyTypes: true. Consider adding 'undefined' to the type of the target.": {
1885+
"Type '{0}' is not assignable to type '{1}' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the type of the target.": {
18861886
"category": "Error",
18871887
"code": 2412
18881888
},

src/services/codefixes/addOptionalPropertyUndefined.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ namespace ts.codefix {
3636
untouched = false;
3737
}
3838
}
39-
if (untouched) {addUndefinedToOptionalProperty(changes, toAdd);}
39+
if (untouched) {
40+
addUndefinedToOptionalProperty(changes, toAdd);
41+
}
4042
});
4143
}));
4244
},
@@ -45,18 +47,18 @@ namespace ts.codefix {
4547
function getPropertiesToAdd(file: SourceFile, pos: number, checker: TypeChecker): Symbol[] {
4648
const sourceTarget = getSourceTarget(getErrorNode(file, pos), checker);
4749
if (!sourceTarget) {
48-
return [];
50+
return emptyArray;
4951
}
5052
const { source: sourceNode, target: targetNode } = sourceTarget;
5153
const target = checker.getTypeAtLocation(targetNode);
5254
const source = checker.getTypeAtLocation(sourceNode);
5355
if (target.symbol?.declarations?.some(d => getSourceFileOfNode(d).fileName.match(/\.d\.ts$/))) {
54-
return [];
56+
return emptyArray;
5557
}
5658
const targetPropertyType = getTargetPropertyType(checker, targetNode);
5759
if (targetPropertyType && checker.isExactOptionalPropertyMismatch(source, targetPropertyType)) {
5860
const s = checker.getSymbolAtLocation((targetNode as PropertyAccessExpression).name);
59-
return s ? [s] : [];
61+
return s ? [s] : emptyArray;
6062
}
6163
return checker.getExactOptionalUnassignableProperties(source, target);
6264
}

tests/baselines/reference/strictOptionalProperties1.errors.txt

Lines changed: 43 additions & 32 deletions
Large diffs are not rendered by default.

tests/baselines/reference/strictOptionalProperties1.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,13 @@ interface U2 {
190190
}
191191
declare const e: string | boolean | undefined
192192
declare const u1: U1
193-
declare const u2: U2
193+
declare let u2: U2
194194
u1.email = e // error, but only because boolean isn't in email's type
195195
u2.email = e // error, and suggest adding undefined
196+
u2 = {
197+
name: 'hi',
198+
email: undefined
199+
}
196200

197201

198202

@@ -326,6 +330,10 @@ function aa(input) {
326330
}
327331
u1.email = e; // error, but only because boolean isn't in email's type
328332
u2.email = e; // error, and suggest adding undefined
333+
u2 = {
334+
name: 'hi',
335+
email: undefined
336+
};
329337

330338

331339
//// [strictOptionalProperties1.d.ts]
@@ -409,4 +417,4 @@ interface U2 {
409417
}
410418
declare const e: string | boolean | undefined;
411419
declare const u1: U1;
412-
declare const u2: U2;
420+
declare let u2: U2;

tests/baselines/reference/strictOptionalProperties1.symbols

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ declare const u1: U1
596596
>u1 : Symbol(u1, Decl(strictOptionalProperties1.ts, 190, 13))
597597
>U1 : Symbol(U1, Decl(strictOptionalProperties1.ts, 179, 41))
598598

599-
declare const u2: U2
600-
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 13))
599+
declare let u2: U2
600+
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 11))
601601
>U2 : Symbol(U2, Decl(strictOptionalProperties1.ts, 184, 1))
602602

603603
u1.email = e // error, but only because boolean isn't in email's type
@@ -608,8 +608,19 @@ u1.email = e // error, but only because boolean isn't in email's type
608608

609609
u2.email = e // error, and suggest adding undefined
610610
>u2.email : Symbol(U2.email, Decl(strictOptionalProperties1.ts, 186, 16))
611-
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 13))
611+
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 11))
612612
>email : Symbol(U2.email, Decl(strictOptionalProperties1.ts, 186, 16))
613613
>e : Symbol(e, Decl(strictOptionalProperties1.ts, 189, 13))
614614

615+
u2 = {
616+
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 11))
617+
618+
name: 'hi',
619+
>name : Symbol(name, Decl(strictOptionalProperties1.ts, 194, 6))
620+
621+
email: undefined
622+
>email : Symbol(email, Decl(strictOptionalProperties1.ts, 195, 15))
623+
>undefined : Symbol(undefined)
624+
}
625+
615626

tests/baselines/reference/strictOptionalProperties1.types

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ declare const e: string | boolean | undefined
703703
declare const u1: U1
704704
>u1 : U1
705705

706-
declare const u2: U2
706+
declare let u2: U2
707707
>u2 : U2
708708

709709
u1.email = e // error, but only because boolean isn't in email's type
@@ -720,4 +720,18 @@ u2.email = e // error, and suggest adding undefined
720720
>email : string | number
721721
>e : string | boolean | undefined
722722

723+
u2 = {
724+
>u2 = { name: 'hi', email: undefined} : { name: string; email: undefined; }
725+
>u2 : U2
726+
>{ name: 'hi', email: undefined} : { name: string; email: undefined; }
727+
728+
name: 'hi',
729+
>name : string
730+
>'hi' : "hi"
731+
732+
email: undefined
733+
>email : undefined
734+
>undefined : undefined
735+
}
736+
723737

tests/cases/compiler/strictOptionalProperties1.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ interface U2 {
193193
}
194194
declare const e: string | boolean | undefined
195195
declare const u1: U1
196-
declare const u2: U2
196+
declare let u2: U2
197197
u1.email = e // error, but only because boolean isn't in email's type
198198
u2.email = e // error, and suggest adding undefined
199+
u2 = {
200+
name: 'hi',
201+
email: undefined
202+
}
199203

0 commit comments

Comments
 (0)