Skip to content

Commit 960e657

Browse files
committed
add test + switch to tracking number symbol ids
1 parent f018b8e commit 960e657

File tree

6 files changed

+151
-3
lines changed

6 files changed

+151
-3
lines changed

src/services/codefixes/addOptionalPropertyUndefined.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace ts.codefix {
2323
getAllCodeActions: context => {
2424
const { program } = context;
2525
const checker = program.getTypeChecker();
26-
const seen = new Map<string, true>();
26+
const seen = new Map<number, true>();
2727
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
2828
eachDiagnostic(context, errorCodes, diag => {
2929
const toAdd = getPropertiesToAdd(diag.file, diag.start, checker);
@@ -32,7 +32,7 @@ namespace ts.codefix {
3232
}
3333
let untouched = true;
3434
for (const add of toAdd) {
35-
if (!addToSeen(seen, getSymbolId(add) + "")) {
35+
if (!addToSeen(seen, getSymbolId(add))) {
3636
untouched = false;
3737
}
3838
}

tests/baselines/reference/strictOptionalProperties1.errors.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ tests/cases/compiler/strictOptionalProperties1.ts(111,7): error TS2375: Type '{
3030
Types of property 'foo' are incompatible.
3131
Type 'undefined' is not assignable to type 'number'.
3232
tests/cases/compiler/strictOptionalProperties1.ts(119,5): error TS2411: Property 'bar' of type 'string | undefined' is not assignable to 'string' index type 'string'.
33+
tests/cases/compiler/strictOptionalProperties1.ts(193,1): error TS2322: Type 'string | boolean | undefined' is not assignable to type 'string | number | undefined'.
34+
Type 'false' is not assignable to type 'string | number | undefined'.
35+
tests/cases/compiler/strictOptionalProperties1.ts(194,1): error TS2412: Type 'string | boolean | undefined' is not assignable to type 'string | number' with exactOptionalPropertyTypes: true. Consider adding 'undefined' to the type of the target.
36+
Type 'undefined' is not assignable to type 'string | number'.
3337

3438

35-
==== tests/cases/compiler/strictOptionalProperties1.ts (16 errors) ====
39+
==== tests/cases/compiler/strictOptionalProperties1.ts (18 errors) ====
3640
function f1(obj: { a?: string, b?: string | undefined }) {
3741
let a = obj.a; // string | undefined
3842
let b = obj.b; // string | undefined
@@ -261,4 +265,25 @@ tests/cases/compiler/strictOptionalProperties1.ts(119,5): error TS2411: Property
261265
}
262266

263267
declare function bb(input: number): void;
268+
269+
interface U1 {
270+
name: string
271+
email?: string | number | undefined
272+
}
273+
interface U2 {
274+
name: string
275+
email?: string | number
276+
}
277+
declare const e: string | boolean | undefined
278+
declare const u1: U1
279+
declare const u2: U2
280+
u1.email = e // error, but only because boolean isn't in email's type
281+
~~~~~~~~
282+
!!! error TS2322: Type 'string | boolean | undefined' is not assignable to type 'string | number | undefined'.
283+
!!! error TS2322: Type 'false' is not assignable to type 'string | number | undefined'.
284+
u2.email = e // error, and suggest adding undefined
285+
~~~~~~~~
286+
!!! error TS2412: Type 'string | boolean | undefined' is not assignable to type 'string | number' with exactOptionalPropertyTypes: true. Consider adding 'undefined' to the type of the target.
287+
!!! error TS2412: Type 'undefined' is not assignable to type 'string | number'.
288+
264289

tests/baselines/reference/strictOptionalProperties1.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ function aa(input: Bar): void {
179179
}
180180

181181
declare function bb(input: number): void;
182+
183+
interface U1 {
184+
name: string
185+
email?: string | number | undefined
186+
}
187+
interface U2 {
188+
name: string
189+
email?: string | number
190+
}
191+
declare const e: string | boolean | undefined
192+
declare const u1: U1
193+
declare const u2: U2
194+
u1.email = e // error, but only because boolean isn't in email's type
195+
u2.email = e // error, and suggest adding undefined
196+
182197

183198

184199
//// [strictOptionalProperties1.js]
@@ -309,6 +324,8 @@ function aa(input) {
309324
var notUndefinedVal = expectNotUndefined(input.bar);
310325
bb(notUndefinedVal);
311326
}
327+
u1.email = e; // error, but only because boolean isn't in email's type
328+
u2.email = e; // error, and suggest adding undefined
312329

313330

314331
//// [strictOptionalProperties1.d.ts]
@@ -382,3 +399,14 @@ interface Bar {
382399
}
383400
declare function aa(input: Bar): void;
384401
declare function bb(input: number): void;
402+
interface U1 {
403+
name: string;
404+
email?: string | number | undefined;
405+
}
406+
interface U2 {
407+
name: string;
408+
email?: string | number;
409+
}
410+
declare const e: string | boolean | undefined;
411+
declare const u1: U1;
412+
declare const u2: U2;

tests/baselines/reference/strictOptionalProperties1.symbols

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,45 @@ declare function bb(input: number): void;
571571
>bb : Symbol(bb, Decl(strictOptionalProperties1.ts, 177, 1))
572572
>input : Symbol(input, Decl(strictOptionalProperties1.ts, 179, 20))
573573

574+
interface U1 {
575+
>U1 : Symbol(U1, Decl(strictOptionalProperties1.ts, 179, 41))
576+
577+
name: string
578+
>name : Symbol(U1.name, Decl(strictOptionalProperties1.ts, 181, 14))
579+
580+
email?: string | number | undefined
581+
>email : Symbol(U1.email, Decl(strictOptionalProperties1.ts, 182, 16))
582+
}
583+
interface U2 {
584+
>U2 : Symbol(U2, Decl(strictOptionalProperties1.ts, 184, 1))
585+
586+
name: string
587+
>name : Symbol(U2.name, Decl(strictOptionalProperties1.ts, 185, 14))
588+
589+
email?: string | number
590+
>email : Symbol(U2.email, Decl(strictOptionalProperties1.ts, 186, 16))
591+
}
592+
declare const e: string | boolean | undefined
593+
>e : Symbol(e, Decl(strictOptionalProperties1.ts, 189, 13))
594+
595+
declare const u1: U1
596+
>u1 : Symbol(u1, Decl(strictOptionalProperties1.ts, 190, 13))
597+
>U1 : Symbol(U1, Decl(strictOptionalProperties1.ts, 179, 41))
598+
599+
declare const u2: U2
600+
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 13))
601+
>U2 : Symbol(U2, Decl(strictOptionalProperties1.ts, 184, 1))
602+
603+
u1.email = e // error, but only because boolean isn't in email's type
604+
>u1.email : Symbol(U1.email, Decl(strictOptionalProperties1.ts, 182, 16))
605+
>u1 : Symbol(u1, Decl(strictOptionalProperties1.ts, 190, 13))
606+
>email : Symbol(U1.email, Decl(strictOptionalProperties1.ts, 182, 16))
607+
>e : Symbol(e, Decl(strictOptionalProperties1.ts, 189, 13))
608+
609+
u2.email = e // error, and suggest adding undefined
610+
>u2.email : Symbol(U2.email, Decl(strictOptionalProperties1.ts, 186, 16))
611+
>u2 : Symbol(u2, Decl(strictOptionalProperties1.ts, 191, 13))
612+
>email : Symbol(U2.email, Decl(strictOptionalProperties1.ts, 186, 16))
613+
>e : Symbol(e, Decl(strictOptionalProperties1.ts, 189, 13))
614+
615+

tests/baselines/reference/strictOptionalProperties1.types

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,41 @@ declare function bb(input: number): void;
683683
>bb : (input: number) => void
684684
>input : number
685685

686+
interface U1 {
687+
name: string
688+
>name : string
689+
690+
email?: string | number | undefined
691+
>email : string | number | undefined
692+
}
693+
interface U2 {
694+
name: string
695+
>name : string
696+
697+
email?: string | number
698+
>email : string | number | undefined
699+
}
700+
declare const e: string | boolean | undefined
701+
>e : string | boolean | undefined
702+
703+
declare const u1: U1
704+
>u1 : U1
705+
706+
declare const u2: U2
707+
>u2 : U2
708+
709+
u1.email = e // error, but only because boolean isn't in email's type
710+
>u1.email = e : string | boolean | undefined
711+
>u1.email : string | number | undefined
712+
>u1 : U1
713+
>email : string | number | undefined
714+
>e : string | boolean | undefined
715+
716+
u2.email = e // error, and suggest adding undefined
717+
>u2.email = e : string | boolean | undefined
718+
>u2.email : string | number
719+
>u2 : U2
720+
>email : string | number
721+
>e : string | boolean | undefined
722+
723+

tests/cases/compiler/strictOptionalProperties1.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,18 @@ function aa(input: Bar): void {
182182
}
183183

184184
declare function bb(input: number): void;
185+
186+
interface U1 {
187+
name: string
188+
email?: string | number | undefined
189+
}
190+
interface U2 {
191+
name: string
192+
email?: string | number
193+
}
194+
declare const e: string | boolean | undefined
195+
declare const u1: U1
196+
declare const u2: U2
197+
u1.email = e // error, but only because boolean isn't in email's type
198+
u2.email = e // error, and suggest adding undefined
199+

0 commit comments

Comments
 (0)