Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type guard by deep property #38839

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
436 changes: 364 additions & 72 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ namespace ts {

const platform: string = _os.platform();
const useCaseSensitiveFileNames = isFileSystemCaseSensitive();
const realpathSync = _fs.realpathSync.native ?? _fs.realpathSync;
const realpathSync = _fs.realpathSync.native;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad merge? Pretty sure the fallback should still be here.


const fsSupportsRecursiveFsWatch = isNode4OrLater && (process.platform === "win32" || process.platform === "darwin");
const getCurrentDirectory = memoize(() => process.cwd());
Expand Down
2 changes: 1 addition & 1 deletion src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ namespace ts.server {
this.compilerOptions.types = [];
break;
default:
Debug.assertNever(projectService.serverMode);
throw new Error("never");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, should still be assertNever.

}

this.setInternalCompilerOptionsForEmittingJsFiles();
Expand Down
3 changes: 2 additions & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ namespace ts.server {
);
break;
default:
Debug.assertNever(this.projectService.serverMode);
throw new Error("never");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, should still be assertNever.

}
}

Expand Down Expand Up @@ -1516,6 +1516,7 @@ namespace ts.server {
// filter handles case when 'projects' is undefined
projects = filter(projects, p => p.languageServiceEnabled && !p.isOrphan());
if (!ignoreNoProjectError && (!projects || !projects.length) && !symLinkedProjects) {
// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, can't have a //@ts-ignore.

this.projectService.logErrorForScriptInfoNotFound(args.file ?? args.projectFileName);
return Errors.ThrowNoProject();
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ namespace ts.FindAllReferences {
addClassStaticThisReferences(referenceLocation, search, state);
break;
default:
Debug.assertNever(state.specialSearchKind);
throw new Error("never");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, should still be assertNever.

}

// Use the parent symbol if the location is commonjs require syntax on javascript files only.
Expand Down
7 changes: 2 additions & 5 deletions tests/baselines/reference/controlFlowOptionalChain.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(463,9): error TS
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(498,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(501,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(515,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(518,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(567,21): error TS2532: Object is possibly 'undefined'.


==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (61 errors) ====
==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (60 errors) ====
// assignments in shortcutting chain
declare const o: undefined | {
[key: string]: any;
Expand Down Expand Up @@ -697,9 +696,7 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(567,21): error T
!!! error TS2532: Object is possibly 'undefined'.
break;
default:
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
o.foo;
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/controlFlowOptionalChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ function f41(o: Thing | undefined) {
o.foo; // Error
break;
default:
o.foo; // Error
o.foo;
break;
}
}
Expand Down Expand Up @@ -1049,7 +1049,7 @@ function f41(o) {
o.foo; // Error
break;
default:
o.foo; // Error
o.foo;
break;
}
}
Expand Down
12 changes: 1 addition & 11 deletions tests/baselines/reference/controlFlowOptionalChain.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -1438,9 +1438,7 @@ function f23(o: Thing | undefined) {
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))

o.foo; // Error
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
}
else {
o.foo;
Expand All @@ -1460,19 +1458,15 @@ function f23(o: Thing | undefined) {
}
else {
o.foo; // Error
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
}
if (typeof o?.foo == "undefined") {
>o?.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))

o.foo; // Error
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
}
else {
o.foo;
Expand All @@ -1492,9 +1486,7 @@ function f23(o: Thing | undefined) {
}
else {
o.foo; // Error
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
}
}

Expand Down Expand Up @@ -1636,13 +1628,11 @@ function f41(o: Thing | undefined) {
break;
case "undefined":
o.foo; // Error
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 505, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))

break;
default:
o.foo; // Error
o.foo;
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 505, 13))
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
Expand Down
34 changes: 17 additions & 17 deletions tests/baselines/reference/controlFlowOptionalChain.types
Original file line number Diff line number Diff line change
Expand Up @@ -1618,9 +1618,9 @@ function f23(o: Thing | undefined) {
>"undefined" : "undefined"

o.foo; // Error
>o.foo : never
>o : Thing | undefined
>foo : never
>o.foo : any
>o : undefined
>foo : any
}
else {
o.foo;
Expand All @@ -1643,9 +1643,9 @@ function f23(o: Thing | undefined) {
}
else {
o.foo; // Error
>o.foo : never
>o : Thing | undefined
>foo : never
>o.foo : any
>o : undefined
>foo : any
}
if (typeof o?.foo == "undefined") {
>typeof o?.foo == "undefined" : boolean
Expand All @@ -1656,9 +1656,9 @@ function f23(o: Thing | undefined) {
>"undefined" : "undefined"

o.foo; // Error
>o.foo : never
>o : Thing | undefined
>foo : never
>o.foo : any
>o : undefined
>foo : any
}
else {
o.foo;
Expand All @@ -1681,9 +1681,9 @@ function f23(o: Thing | undefined) {
}
else {
o.foo; // Error
>o.foo : never
>o : Thing | undefined
>foo : never
>o.foo : any
>o : undefined
>foo : any
}
}

Expand Down Expand Up @@ -1852,15 +1852,15 @@ function f41(o: Thing | undefined) {
>"undefined" : "undefined"

o.foo; // Error
>o.foo : never
>o : Thing | undefined
>foo : never
>o.foo : any
>o : undefined
>foo : any

break;
default:
o.foo; // Error
o.foo;
>o.foo : never
>o : Thing | undefined
>o : Thing
>foo : never

break;
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/destructuringControlFlow.types
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,34 @@ function f2(obj: [number, string] | null[]) {
>obj[0] : number | null
>obj : [number, string] | null[]
>0 : 0
>obj[1] : string | null
>obj : [number, string] | null[]
>obj[1] : string
>obj : [number, string]
>1 : 1

let c0 = obj[0]; // number
>c0 : number
>obj[0] : number
>obj : [number, string] | null[]
>obj : [number, string]
>0 : 0

let c1 = obj[1]; // string
>c1 : string
>obj[1] : string
>obj : [number, string] | null[]
>obj : [number, string]
>1 : 1

let [d0, d1] = obj;
>d0 : number
>d1 : string
>obj : [number, string] | null[]
>obj : [number, string]

([c0, c1] = obj);
>([c0, c1] = obj) : [number, string] | null[]
>[c0, c1] = obj : [number, string] | null[]
>([c0, c1] = obj) : [number, string]
>[c0, c1] = obj : [number, string]
>[c0, c1] : [number, string]
>c0 : number
>c1 : string
>obj : [number, string] | null[]
>obj : [number, string]
}
}

Expand Down
7 changes: 5 additions & 2 deletions tests/baselines/reference/discriminatedUnionTypes2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(27,30): error TS
Object literal may only specify known properties, and 'c' does not exist in type '{ a: null; b: string; }'.
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS2339: Property 'b' does not exist on type '{ a: 0; b: string; } | { a: T; c: number; }'.
Property 'b' does not exist on type '{ a: T; c: number; }'.
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(132,11): error TS2339: Property 'value' does not exist on type 'never'.


==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (2 errors) ====
==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (3 errors) ====
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) {
if (x.kind === false) {
x.a;
Expand Down Expand Up @@ -142,7 +143,9 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS
x.value; // number
}
else {
x.value; // number
x.value; // Error, x is never
~~~~~
!!! error TS2339: Property 'value' does not exist on type 'never'.
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/discriminatedUnionTypes2.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
x.value; // number
}
else {
x.value; // number
x.value; // Error, x is never
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ function foo1(x) {
x.value; // number
}
else {
x.value; // number
x.value; // Error, x is never
}
}
function foo2(x) {
Expand Down
4 changes: 1 addition & 3 deletions tests/baselines/reference/discriminatedUnionTypes2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,8 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
}
else {
x.value; // number
>x.value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
x.value; // Error, x is never
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 126, 14))
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/discriminatedUnionTypes2.types
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
>value : number
}
else {
x.value; // number
>x.value : number
>x : { type: "number"; value: number; } & { type: "number"; }
>value : number
x.value; // Error, x is never
>x.value : any
>x : never
>value : any
}
}

Expand Down
Loading