-
Notifications
You must be signed in to change notification settings - Fork 12.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use missingType in noUncheckedIndexedAccess mode * Accept new baselines * Add tests * Optimizing searching for undefinedType and missingType
- Loading branch information
1 parent
91f89b9
commit d43112a
Showing
5 changed files
with
234 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
tests/baselines/reference/inKeywordNarrowingWithNoUncheckedIndexedAccess.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts === | ||
declare function invariant(condition: boolean): asserts condition; | ||
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0)) | ||
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27)) | ||
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27)) | ||
|
||
function f1(obj: Record<string, string>) { | ||
>f1 : Symbol(f1, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 66)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
invariant("test" in obj); | ||
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12)) | ||
|
||
return obj.test; // string | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12)) | ||
} | ||
|
||
function f2(obj: Record<string, string>) { | ||
>f2 : Symbol(f2, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 5, 1)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if ("test" in obj) { | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12)) | ||
|
||
return obj.test; // string | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12)) | ||
} | ||
return "default"; | ||
} | ||
|
||
function f3(obj: Record<string, string>) { | ||
>f3 : Symbol(f3, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 12, 1)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
obj.test; // string | undefined | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12)) | ||
|
||
if ("test" in obj) { | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12)) | ||
|
||
obj.test; // string | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12)) | ||
} | ||
else { | ||
obj.test; // undefined | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12)) | ||
} | ||
} | ||
|
||
function f4(obj: Record<string, string>) { | ||
>f4 : Symbol(f4, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 22, 1)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
obj.test; // string | undefined | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12)) | ||
|
||
if (obj.hasOwnProperty("test")) { | ||
>obj.hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12)) | ||
>hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --)) | ||
|
||
obj.test; // string | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12)) | ||
} | ||
else { | ||
obj.test; // undefined | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12)) | ||
} | ||
} | ||
|
96 changes: 96 additions & 0 deletions
96
tests/baselines/reference/inKeywordNarrowingWithNoUncheckedIndexedAccess.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts === | ||
declare function invariant(condition: boolean): asserts condition; | ||
>invariant : (condition: boolean) => asserts condition | ||
>condition : boolean | ||
|
||
function f1(obj: Record<string, string>) { | ||
>f1 : (obj: Record<string, string>) => string | ||
>obj : Record<string, string> | ||
|
||
invariant("test" in obj); | ||
>invariant("test" in obj) : void | ||
>invariant : (condition: boolean) => asserts condition | ||
>"test" in obj : boolean | ||
>"test" : "test" | ||
>obj : Record<string, string> | ||
|
||
return obj.test; // string | ||
>obj.test : string | ||
>obj : Record<string, string> | ||
>test : string | ||
} | ||
|
||
function f2(obj: Record<string, string>) { | ||
>f2 : (obj: Record<string, string>) => string | ||
>obj : Record<string, string> | ||
|
||
if ("test" in obj) { | ||
>"test" in obj : boolean | ||
>"test" : "test" | ||
>obj : Record<string, string> | ||
|
||
return obj.test; // string | ||
>obj.test : string | ||
>obj : Record<string, string> | ||
>test : string | ||
} | ||
return "default"; | ||
>"default" : "default" | ||
} | ||
|
||
function f3(obj: Record<string, string>) { | ||
>f3 : (obj: Record<string, string>) => void | ||
>obj : Record<string, string> | ||
|
||
obj.test; // string | undefined | ||
>obj.test : string | undefined | ||
>obj : Record<string, string> | ||
>test : string | undefined | ||
|
||
if ("test" in obj) { | ||
>"test" in obj : boolean | ||
>"test" : "test" | ||
>obj : Record<string, string> | ||
|
||
obj.test; // string | ||
>obj.test : string | ||
>obj : Record<string, string> | ||
>test : string | ||
} | ||
else { | ||
obj.test; // undefined | ||
>obj.test : undefined | ||
>obj : Record<string, string> | ||
>test : undefined | ||
} | ||
} | ||
|
||
function f4(obj: Record<string, string>) { | ||
>f4 : (obj: Record<string, string>) => void | ||
>obj : Record<string, string> | ||
|
||
obj.test; // string | undefined | ||
>obj.test : string | undefined | ||
>obj : Record<string, string> | ||
>test : string | undefined | ||
|
||
if (obj.hasOwnProperty("test")) { | ||
>obj.hasOwnProperty("test") : boolean | ||
>obj.hasOwnProperty : (v: PropertyKey) => boolean | ||
>obj : Record<string, string> | ||
>hasOwnProperty : (v: PropertyKey) => boolean | ||
>"test" : "test" | ||
|
||
obj.test; // string | ||
>obj.test : string | ||
>obj : Record<string, string> | ||
>test : string | ||
} | ||
else { | ||
obj.test; // undefined | ||
>obj.test : undefined | ||
>obj : Record<string, string> | ||
>test : undefined | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
// @noUncheckedIndexedAccess: true | ||
|
||
declare function invariant(condition: boolean): asserts condition; | ||
|
||
function f1(obj: Record<string, string>) { | ||
invariant("test" in obj); | ||
return obj.test; // string | ||
} | ||
|
||
function f2(obj: Record<string, string>) { | ||
if ("test" in obj) { | ||
return obj.test; // string | ||
} | ||
return "default"; | ||
} | ||
|
||
function f3(obj: Record<string, string>) { | ||
obj.test; // string | undefined | ||
if ("test" in obj) { | ||
obj.test; // string | ||
} | ||
else { | ||
obj.test; // undefined | ||
} | ||
} | ||
|
||
function f4(obj: Record<string, string>) { | ||
obj.test; // string | undefined | ||
if (obj.hasOwnProperty("test")) { | ||
obj.test; // string | ||
} | ||
else { | ||
obj.test; // undefined | ||
} | ||
} |