-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
762a027
commit 52a8e26
Showing
12 changed files
with
416 additions
and
128 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
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
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
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
154 changes: 154 additions & 0 deletions
154
tests/baselines/reference/typeGuardAccordingToPropertyOptional.errors.txt
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,154 @@ | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(64,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(72,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(84,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(84,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(88,11): error TS2339: Property 'key1' does not exist on type 'never'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(96,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(96,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(103,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(103,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(115,9): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts(115,9): error TS2532: Object is possibly 'undefined'. | ||
|
||
|
||
==== tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyOptional.ts (11 errors) ==== | ||
interface Foo1 { | ||
key1:{ | ||
key2:number; | ||
}|undefined; | ||
f1: number; | ||
} | ||
|
||
interface Foo2 { | ||
key1: { | ||
key2:string | ||
} | undefined; | ||
f2: number; | ||
} | ||
|
||
interface Foo3 { | ||
key1:{ | ||
key2:number; | ||
}; | ||
f2: number; | ||
} | ||
|
||
type U1 = Foo1 | Foo2 | Foo3; | ||
type U2 = Foo1 | Foo2 | Foo3|undefined; | ||
|
||
// unnecessary optional chain | ||
function f1(u: U1) { | ||
if (typeof u?.key1 !== 'number') { | ||
u; // U1 | ||
} | ||
if (typeof u?.key1 === 'number') { | ||
u; // never | ||
} | ||
if (typeof u?.key1 !== 'undefined') { | ||
u; // U1 | ||
} | ||
if (typeof u?.key1 === 'undefined') { | ||
u; // U1 | ||
} | ||
} | ||
|
||
// non-root optional chain | ||
function f2(u: U1) { | ||
if (typeof u.key1?.key2 !== 'number') { | ||
u; // U1 | ||
} | ||
if (typeof u.key1?.key2 === 'number') { | ||
u; // Foo1 | Foo3 | ||
} | ||
if (typeof u.key1?.key2 !== 'undefined') { | ||
u; // U1 | ||
} | ||
if (typeof u.key1?.key2 === 'undefined') { | ||
u; // U1 | ||
} | ||
} | ||
|
||
function f2Plus(u: U1){ | ||
if (typeof u.key1?.key2 === 'undefined' && typeof u.key1?.key2 === 'number') { | ||
u; // Foo1 | Foo3 | ||
u.key1.key2; | ||
} | ||
if (typeof u.key1?.key2 === 'undefined' || typeof u.key1?.key2 === 'number') { | ||
u; // U1 | ||
u.key1.key2; // Error | ||
~~~~~~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
} | ||
if ( typeof u.key1?.key2 === 'number' && typeof u.key1?.key2 === 'undefined') { | ||
u; // Foo1 | Foo3 | ||
u.key1.key2; | ||
} | ||
if ( typeof u.key1?.key2 === 'number' || typeof u.key1?.key2 === 'undefined') { | ||
u; // U1 | ||
u.key1.key2; // Error | ||
~~~~~~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
} | ||
if ( typeof u.key1?.key2 === 'number' || typeof u.key1?.key2 !== 'undefined') { | ||
u; // U1 | ||
u.key1.key2; | ||
} | ||
} | ||
|
||
// root optional chain | ||
function f3(u: U2) { | ||
if (typeof u?.key1 !== 'number') { | ||
u; // U2 | ||
u.key1.key2; | ||
~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
~~~~~~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
} | ||
if (typeof u?.key1 === 'number') { | ||
u; // never | ||
u.key1.key2; | ||
~~~~ | ||
!!! error TS2339: Property 'key1' does not exist on type 'never'. | ||
} | ||
if (typeof u?.key1 !== 'undefined') { | ||
u; // Foo1 | Foo2 | Foo3 | ||
u.key1.key2; | ||
} | ||
if (typeof u?.key1 === 'undefined') { | ||
u; // U2 | ||
u.key1.key2; | ||
~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
~~~~~~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
} | ||
} | ||
|
||
function f4(u: U2) { | ||
if (typeof u?.key1?.key2 !== 'number') { | ||
u; // U2 | ||
u.key1.key2; | ||
~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
~~~~~~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
} | ||
if (typeof u?.key1?.key2 === 'number') { | ||
u; // Foo1 | Foo3 | ||
u.key1.key2; | ||
} | ||
if (typeof u?.key1?.key2 !== 'undefined') { | ||
u; // Foo1 | Foo2 | Foo3 | ||
u.key1.key2; | ||
} | ||
if (typeof u?.key1?.key2 === 'undefined') { | ||
u; // U2 | ||
u.key1.key2; | ||
~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
~~~~~~ | ||
!!! error TS2532: Object is possibly '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
Oops, something went wrong.