Skip to content

Fix destructuring and narrowing interaction #47337

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

Merged
merged 17 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
77 changes: 50 additions & 27 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,17 @@ namespace FourSlash {
}
}

public verifyTypeAtLocation(range: Range, expected: string): void {
const checker = this.getChecker();
const node = this.goToAndGetNode(range);
const type = checker.getTypeAtLocation(node);

const actual = checker.typeToString(type);
if (actual !== expected) {
this.raiseError(displayExpectedAndActualString(expected, actual));
}
}

private verifyDocumentHighlightsRespectFilesList(files: readonly string[]): void {
const startFile = this.activeFile.fileName;
for (const fileName of files) {
Expand Down
4 changes: 4 additions & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ namespace FourSlashInterface {
this.state.verifyTypeOfSymbolAtLocation(range, symbol, expected);
}

public typeAtLocation(range: FourSlash.Range, expected: string): void {
this.state.verifyTypeAtLocation(range, expected);
}

public baselineFindAllReferences(...markerNames: string[]) {
this.state.verifyBaselineFindAllReferences(...markerNames);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getType = <P extends Params>(params: P) => {
>rest : Omit<P, "foo">

} = params;
>params : P
>params : Params

return rest;
>rest : Omit<P, "foo">
Expand Down
89 changes: 89 additions & 0 deletions tests/baselines/reference/narrowingDestructuring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//// [narrowingDestructuring.ts]
type X = { kind: "a", a: string } | { kind: "b", b: string }

function func<T extends X>(value: T) {
if (value.kind === "a") {
value.a;
const { a } = value;
} else {
value.b;
const { b } = value;
}
}

type Z = { kind: "f", f: { a: number, b: string, c: number } }
| { kind: "g", g: { a: string, b: number, c: string }};

function func2<T extends Z>(value: T) {
if (value.kind === "f") {
const { f: f1 } = value;
const { f: { a, ...spread } } = value;
value.f;
} else {
const { g: { c, ...spread } } = value;
value.g;
}
}

function func3<T extends { kind: "a", a: string } | { kind: "b", b: number }>(t: T) {
if (t.kind === "a") {
const { kind, ...r1 } = t;
const r2 = (({ kind, ...rest }) => rest)(t);
}
}

function farr<T extends [number, string, string] | [string, number, number]>(x: T) {
const [head, ...tail] = x;
if (x[0] === 'number') {
const [head, ...tail] = x;
}
}

//// [narrowingDestructuring.js]
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
function func(value) {
if (value.kind === "a") {
value.a;
var a = value.a;
}
else {
value.b;
var b = value.b;
}
}
function func2(value) {
if (value.kind === "f") {
var f1 = value.f;
var _a = value.f, a = _a.a, spread = __rest(_a, ["a"]);
value.f;
}
else {
var _b = value.g, c = _b.c, spread = __rest(_b, ["c"]);
value.g;
}
}
function func3(t) {
if (t.kind === "a") {
var kind = t.kind, r1 = __rest(t, ["kind"]);
var r2 = (function (_a) {
var kind = _a.kind, rest = __rest(_a, ["kind"]);
return rest;
})(t);
}
}
function farr(x) {
var head = x[0], tail = x.slice(1);
if (x[0] === 'number') {
var head_1 = x[0], tail_1 = x.slice(1);
}
}
148 changes: 148 additions & 0 deletions tests/baselines/reference/narrowingDestructuring.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
=== tests/cases/compiler/narrowingDestructuring.ts ===
type X = { kind: "a", a: string } | { kind: "b", b: string }
>X : Symbol(X, Decl(narrowingDestructuring.ts, 0, 0))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 0, 10))
>a : Symbol(a, Decl(narrowingDestructuring.ts, 0, 21))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 0, 37))
>b : Symbol(b, Decl(narrowingDestructuring.ts, 0, 48))

function func<T extends X>(value: T) {
>func : Symbol(func, Decl(narrowingDestructuring.ts, 0, 60))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 2, 14))
>X : Symbol(X, Decl(narrowingDestructuring.ts, 0, 0))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 2, 27))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 2, 14))

if (value.kind === "a") {
>value.kind : Symbol(kind, Decl(narrowingDestructuring.ts, 0, 10), Decl(narrowingDestructuring.ts, 0, 37))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 2, 27))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 0, 10), Decl(narrowingDestructuring.ts, 0, 37))

value.a;
>value.a : Symbol(a, Decl(narrowingDestructuring.ts, 0, 21))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 2, 27))
>a : Symbol(a, Decl(narrowingDestructuring.ts, 0, 21))

const { a } = value;
>a : Symbol(a, Decl(narrowingDestructuring.ts, 5, 15))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 2, 27))

} else {
value.b;
>value.b : Symbol(b, Decl(narrowingDestructuring.ts, 0, 48))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 2, 27))
>b : Symbol(b, Decl(narrowingDestructuring.ts, 0, 48))

const { b } = value;
>b : Symbol(b, Decl(narrowingDestructuring.ts, 8, 15))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 2, 27))
}
}

type Z = { kind: "f", f: { a: number, b: string, c: number } }
>Z : Symbol(Z, Decl(narrowingDestructuring.ts, 10, 1))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 12, 10))
>f : Symbol(f, Decl(narrowingDestructuring.ts, 12, 21))
>a : Symbol(a, Decl(narrowingDestructuring.ts, 12, 26))
>b : Symbol(b, Decl(narrowingDestructuring.ts, 12, 37))
>c : Symbol(c, Decl(narrowingDestructuring.ts, 12, 48))

| { kind: "g", g: { a: string, b: number, c: string }};
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 13, 7))
>g : Symbol(g, Decl(narrowingDestructuring.ts, 13, 18))
>a : Symbol(a, Decl(narrowingDestructuring.ts, 13, 23))
>b : Symbol(b, Decl(narrowingDestructuring.ts, 13, 34))
>c : Symbol(c, Decl(narrowingDestructuring.ts, 13, 45))

function func2<T extends Z>(value: T) {
>func2 : Symbol(func2, Decl(narrowingDestructuring.ts, 13, 59))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 15, 15))
>Z : Symbol(Z, Decl(narrowingDestructuring.ts, 10, 1))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 15, 15))

if (value.kind === "f") {
>value.kind : Symbol(kind, Decl(narrowingDestructuring.ts, 12, 10), Decl(narrowingDestructuring.ts, 13, 7))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 12, 10), Decl(narrowingDestructuring.ts, 13, 7))

const { f: f1 } = value;
>f : Symbol(f, Decl(narrowingDestructuring.ts, 12, 21))
>f1 : Symbol(f1, Decl(narrowingDestructuring.ts, 17, 15))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))

const { f: { a, ...spread } } = value;
>f : Symbol(f, Decl(narrowingDestructuring.ts, 12, 21))
>a : Symbol(a, Decl(narrowingDestructuring.ts, 18, 20))
>spread : Symbol(spread, Decl(narrowingDestructuring.ts, 18, 23))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))

value.f;
>value.f : Symbol(f, Decl(narrowingDestructuring.ts, 12, 21))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))
>f : Symbol(f, Decl(narrowingDestructuring.ts, 12, 21))

} else {
const { g: { c, ...spread } } = value;
>g : Symbol(g, Decl(narrowingDestructuring.ts, 13, 18))
>c : Symbol(c, Decl(narrowingDestructuring.ts, 21, 20))
>spread : Symbol(spread, Decl(narrowingDestructuring.ts, 21, 23))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))

value.g;
>value.g : Symbol(g, Decl(narrowingDestructuring.ts, 13, 18))
>value : Symbol(value, Decl(narrowingDestructuring.ts, 15, 28))
>g : Symbol(g, Decl(narrowingDestructuring.ts, 13, 18))
}
}

function func3<T extends { kind: "a", a: string } | { kind: "b", b: number }>(t: T) {
>func3 : Symbol(func3, Decl(narrowingDestructuring.ts, 24, 1))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 26, 15))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 26, 26))
>a : Symbol(a, Decl(narrowingDestructuring.ts, 26, 37))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 26, 53))
>b : Symbol(b, Decl(narrowingDestructuring.ts, 26, 64))
>t : Symbol(t, Decl(narrowingDestructuring.ts, 26, 78))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 26, 15))

if (t.kind === "a") {
>t.kind : Symbol(kind, Decl(narrowingDestructuring.ts, 26, 26), Decl(narrowingDestructuring.ts, 26, 53))
>t : Symbol(t, Decl(narrowingDestructuring.ts, 26, 78))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 26, 26), Decl(narrowingDestructuring.ts, 26, 53))

const { kind, ...r1 } = t;
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 28, 15))
>r1 : Symbol(r1, Decl(narrowingDestructuring.ts, 28, 21))
>t : Symbol(t, Decl(narrowingDestructuring.ts, 26, 78))

const r2 = (({ kind, ...rest }) => rest)(t);
>r2 : Symbol(r2, Decl(narrowingDestructuring.ts, 29, 13))
>kind : Symbol(kind, Decl(narrowingDestructuring.ts, 29, 22))
>rest : Symbol(rest, Decl(narrowingDestructuring.ts, 29, 28))
>rest : Symbol(rest, Decl(narrowingDestructuring.ts, 29, 28))
>t : Symbol(t, Decl(narrowingDestructuring.ts, 26, 78))
}
}

function farr<T extends [number, string, string] | [string, number, number]>(x: T) {
>farr : Symbol(farr, Decl(narrowingDestructuring.ts, 31, 1))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 33, 14))
>x : Symbol(x, Decl(narrowingDestructuring.ts, 33, 77))
>T : Symbol(T, Decl(narrowingDestructuring.ts, 33, 14))

const [head, ...tail] = x;
>head : Symbol(head, Decl(narrowingDestructuring.ts, 34, 11))
>tail : Symbol(tail, Decl(narrowingDestructuring.ts, 34, 16))
>x : Symbol(x, Decl(narrowingDestructuring.ts, 33, 77))

if (x[0] === 'number') {
>x : Symbol(x, Decl(narrowingDestructuring.ts, 33, 77))
>0 : Symbol(0)

const [head, ...tail] = x;
>head : Symbol(head, Decl(narrowingDestructuring.ts, 36, 15))
>tail : Symbol(tail, Decl(narrowingDestructuring.ts, 36, 20))
>x : Symbol(x, Decl(narrowingDestructuring.ts, 33, 77))
}
}
Loading