Skip to content

Commit 42cc565

Browse files
committed
Support string literals+computed property names
With tests and associated baseline updates
1 parent 70e9e09 commit 42cc565

10 files changed

Lines changed: 409 additions & 37 deletions

src/compiler/checker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7856,9 +7856,11 @@ namespace ts {
78567856
const name = declaration.propertyName || declaration.name;
78577857
if (isVariableLike(parentDeclaration) &&
78587858
parentDeclaration.type &&
7859-
(name.kind === SyntaxKind.Identifier || name.kind == SyntaxKind.StringLiteral)) {
7860-
const text = (<Identifier | LiteralExpression>name).text;
7861-
return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text);
7859+
!isBindingPattern(name)) {
7860+
const text = getTextOfPropertyName(name);
7861+
if (text) {
7862+
return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text);
7863+
}
78627864
}
78637865
}
78647866
}

tests/baselines/reference/contextuallyTypedBindingInitializer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ interface Show {
33
show: (x: number) => string;
44
}
55
function f({ show = v => v.toString() }: Show) {}
6+
function f2({ "show": showRename = v => v.toString() }: Show) {}
7+
function f3({ ["show"]: showRename = v => v.toString() }: Show) {}
68

79
interface Nested {
810
nested: Show
@@ -23,12 +25,20 @@ interface StringIdentity {
2325
stringIdentity(s: string): string;
2426
}
2527
let { stringIdentity: id = arg => arg }: StringIdentity = { stringIdentity: x => x};
28+
29+
2630

2731

2832
//// [contextuallyTypedBindingInitializer.js]
2933
function f(_a) {
3034
var _b = _a.show, show = _b === void 0 ? function (v) { return v.toString(); } : _b;
3135
}
36+
function f2(_a) {
37+
var _b = _a["show"], showRename = _b === void 0 ? function (v) { return v.toString(); } : _b;
38+
}
39+
function f3(_a) {
40+
var _b = "show", _c = _a[_b], showRename = _c === void 0 ? function (v) { return v.toString(); } : _c;
41+
}
3242
function ff(_a) {
3343
var _b = _a.nested, nested = _b === void 0 ? { show: function (v) { return v.toString(); } } : _b;
3444
}

tests/baselines/reference/contextuallyTypedBindingInitializer.symbols

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,79 @@ function f({ show = v => v.toString() }: Show) {}
1515
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
1616
>Show : Symbol(Show, Decl(contextuallyTypedBindingInitializer.ts, 0, 0))
1717

18+
function f2({ "show": showRename = v => v.toString() }: Show) {}
19+
>f2 : Symbol(f2, Decl(contextuallyTypedBindingInitializer.ts, 3, 49))
20+
>showRename : Symbol(showRename, Decl(contextuallyTypedBindingInitializer.ts, 4, 13))
21+
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 4, 34))
22+
>v.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
23+
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 4, 34))
24+
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
25+
>Show : Symbol(Show, Decl(contextuallyTypedBindingInitializer.ts, 0, 0))
26+
27+
function f3({ ["show"]: showRename = v => v.toString() }: Show) {}
28+
>f3 : Symbol(f3, Decl(contextuallyTypedBindingInitializer.ts, 4, 64))
29+
>showRename : Symbol(showRename, Decl(contextuallyTypedBindingInitializer.ts, 5, 13))
30+
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 5, 36))
31+
>v.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
32+
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 5, 36))
33+
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
34+
>Show : Symbol(Show, Decl(contextuallyTypedBindingInitializer.ts, 0, 0))
35+
1836
interface Nested {
19-
>Nested : Symbol(Nested, Decl(contextuallyTypedBindingInitializer.ts, 3, 49))
37+
>Nested : Symbol(Nested, Decl(contextuallyTypedBindingInitializer.ts, 5, 66))
2038

2139
nested: Show
22-
>nested : Symbol(nested, Decl(contextuallyTypedBindingInitializer.ts, 5, 18))
40+
>nested : Symbol(nested, Decl(contextuallyTypedBindingInitializer.ts, 7, 18))
2341
>Show : Symbol(Show, Decl(contextuallyTypedBindingInitializer.ts, 0, 0))
2442
}
2543
function ff({ nested = { show: v => v.toString() } }: Nested) {}
26-
>ff : Symbol(ff, Decl(contextuallyTypedBindingInitializer.ts, 7, 1))
27-
>nested : Symbol(nested, Decl(contextuallyTypedBindingInitializer.ts, 8, 13))
28-
>show : Symbol(show, Decl(contextuallyTypedBindingInitializer.ts, 8, 24))
29-
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 8, 30))
44+
>ff : Symbol(ff, Decl(contextuallyTypedBindingInitializer.ts, 9, 1))
45+
>nested : Symbol(nested, Decl(contextuallyTypedBindingInitializer.ts, 10, 13))
46+
>show : Symbol(show, Decl(contextuallyTypedBindingInitializer.ts, 10, 24))
47+
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 10, 30))
3048
>v.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
31-
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 8, 30))
49+
>v : Symbol(v, Decl(contextuallyTypedBindingInitializer.ts, 10, 30))
3250
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
33-
>Nested : Symbol(Nested, Decl(contextuallyTypedBindingInitializer.ts, 3, 49))
51+
>Nested : Symbol(Nested, Decl(contextuallyTypedBindingInitializer.ts, 5, 66))
3452

3553
interface Tuples {
36-
>Tuples : Symbol(Tuples, Decl(contextuallyTypedBindingInitializer.ts, 8, 64))
54+
>Tuples : Symbol(Tuples, Decl(contextuallyTypedBindingInitializer.ts, 10, 64))
3755

3856
prop: [string, number];
39-
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 10, 18))
57+
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 12, 18))
4058
}
4159
function g({ prop = ["hello", 1234] }: Tuples) {}
42-
>g : Symbol(g, Decl(contextuallyTypedBindingInitializer.ts, 12, 1))
43-
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 13, 12))
44-
>Tuples : Symbol(Tuples, Decl(contextuallyTypedBindingInitializer.ts, 8, 64))
60+
>g : Symbol(g, Decl(contextuallyTypedBindingInitializer.ts, 14, 1))
61+
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 15, 12))
62+
>Tuples : Symbol(Tuples, Decl(contextuallyTypedBindingInitializer.ts, 10, 64))
4563

4664
interface StringUnion {
47-
>StringUnion : Symbol(StringUnion, Decl(contextuallyTypedBindingInitializer.ts, 13, 49))
65+
>StringUnion : Symbol(StringUnion, Decl(contextuallyTypedBindingInitializer.ts, 15, 49))
4866

4967
prop: "foo" | "bar";
50-
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 15, 23))
68+
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 17, 23))
5169
}
5270
function h({ prop = "foo" }: StringUnion) {}
53-
>h : Symbol(h, Decl(contextuallyTypedBindingInitializer.ts, 17, 1))
54-
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 18, 12))
55-
>StringUnion : Symbol(StringUnion, Decl(contextuallyTypedBindingInitializer.ts, 13, 49))
71+
>h : Symbol(h, Decl(contextuallyTypedBindingInitializer.ts, 19, 1))
72+
>prop : Symbol(prop, Decl(contextuallyTypedBindingInitializer.ts, 20, 12))
73+
>StringUnion : Symbol(StringUnion, Decl(contextuallyTypedBindingInitializer.ts, 15, 49))
5674

5775
interface StringIdentity {
58-
>StringIdentity : Symbol(StringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 18, 44))
76+
>StringIdentity : Symbol(StringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 20, 44))
5977

6078
stringIdentity(s: string): string;
61-
>stringIdentity : Symbol(stringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 20, 26))
62-
>s : Symbol(s, Decl(contextuallyTypedBindingInitializer.ts, 21, 19))
79+
>stringIdentity : Symbol(stringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 22, 26))
80+
>s : Symbol(s, Decl(contextuallyTypedBindingInitializer.ts, 23, 19))
6381
}
6482
let { stringIdentity: id = arg => arg }: StringIdentity = { stringIdentity: x => x};
65-
>stringIdentity : Symbol(StringIdentity.stringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 20, 26))
66-
>id : Symbol(id, Decl(contextuallyTypedBindingInitializer.ts, 23, 5))
67-
>arg : Symbol(arg, Decl(contextuallyTypedBindingInitializer.ts, 23, 26))
68-
>arg : Symbol(arg, Decl(contextuallyTypedBindingInitializer.ts, 23, 26))
69-
>StringIdentity : Symbol(StringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 18, 44))
70-
>stringIdentity : Symbol(stringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 23, 59))
71-
>x : Symbol(x, Decl(contextuallyTypedBindingInitializer.ts, 23, 75))
72-
>x : Symbol(x, Decl(contextuallyTypedBindingInitializer.ts, 23, 75))
83+
>stringIdentity : Symbol(StringIdentity.stringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 22, 26))
84+
>id : Symbol(id, Decl(contextuallyTypedBindingInitializer.ts, 25, 5))
85+
>arg : Symbol(arg, Decl(contextuallyTypedBindingInitializer.ts, 25, 26))
86+
>arg : Symbol(arg, Decl(contextuallyTypedBindingInitializer.ts, 25, 26))
87+
>StringIdentity : Symbol(StringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 20, 44))
88+
>stringIdentity : Symbol(stringIdentity, Decl(contextuallyTypedBindingInitializer.ts, 25, 59))
89+
>x : Symbol(x, Decl(contextuallyTypedBindingInitializer.ts, 25, 75))
90+
>x : Symbol(x, Decl(contextuallyTypedBindingInitializer.ts, 25, 75))
91+
92+
7393

tests/baselines/reference/contextuallyTypedBindingInitializer.types

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ function f({ show = v => v.toString() }: Show) {}
1717
>toString : (radix?: number) => string
1818
>Show : Show
1919

20+
function f2({ "show": showRename = v => v.toString() }: Show) {}
21+
>f2 : ({"show": showRename}: Show) => void
22+
>showRename : (x: number) => string
23+
>v => v.toString() : (v: number) => string
24+
>v : number
25+
>v.toString() : string
26+
>v.toString : (radix?: number) => string
27+
>v : number
28+
>toString : (radix?: number) => string
29+
>Show : Show
30+
31+
function f3({ ["show"]: showRename = v => v.toString() }: Show) {}
32+
>f3 : ({["show"]: showRename}: Show) => void
33+
>"show" : string
34+
>showRename : (x: number) => string
35+
>v => v.toString() : (v: number) => string
36+
>v : number
37+
>v.toString() : string
38+
>v.toString : (radix?: number) => string
39+
>v : number
40+
>toString : (radix?: number) => string
41+
>Show : Show
42+
2043
interface Nested {
2144
>Nested : Nested
2245

@@ -83,3 +106,5 @@ let { stringIdentity: id = arg => arg }: StringIdentity = { stringIdentity: x =>
83106
>x : string
84107
>x : string
85108

109+
110+

tests/baselines/reference/contextuallyTypedBindingInitializerNegative.errors.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(4,20): error TS2322: Type '(v: number) => number' is not assignable to type '(x: number) => string'.
22
Type 'number' is not assignable to type 'string'.
3-
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(9,23): error TS2322: Type '{ show: (v: number) => number; }' is not assignable to type 'Show'.
3+
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(5,23): error TS2322: Type '(v: number) => number' is not assignable to type '(x: number) => string'.
4+
Type 'number' is not assignable to type 'string'.
5+
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(6,25): error TS2322: Type '(v: number) => number' is not assignable to type '(x: number) => string'.
6+
Type 'number' is not assignable to type 'string'.
7+
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(11,23): error TS2322: Type '{ show: (v: number) => number; }' is not assignable to type 'Show'.
48
Types of property 'show' are incompatible.
59
Type '(v: number) => number' is not assignable to type '(x: number) => string'.
610
Type 'number' is not assignable to type 'string'.
7-
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(14,23): error TS2322: Type '(arg: string) => number' is not assignable to type '(s: string) => string'.
11+
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(16,23): error TS2322: Type '(arg: string) => number' is not assignable to type '(s: string) => string'.
812
Type 'number' is not assignable to type 'string'.
9-
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(19,14): error TS2322: Type '[number, number]' is not assignable to type '[string, number]'.
13+
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(21,14): error TS2322: Type '[number, number]' is not assignable to type '[string, number]'.
1014
Types of property '0' are incompatible.
1115
Type 'number' is not assignable to type 'string'.
12-
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(24,14): error TS2322: Type '"baz"' is not assignable to type '"foo" | "bar"'.
16+
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(26,14): error TS2322: Type '"baz"' is not assignable to type '"foo" | "bar"'.
1317
Type '"baz"' is not assignable to type '"bar"'.
1418

1519

16-
==== tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts (5 errors) ====
20+
==== tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts (7 errors) ====
1721
interface Show {
1822
show: (x: number) => string;
1923
}
2024
function f({ show: showRename = v => v }: Show) {}
2125
~~~~~~~~~~
2226
!!! error TS2322: Type '(v: number) => number' is not assignable to type '(x: number) => string'.
27+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
28+
function f2({ "show": showRename = v => v }: Show) {}
29+
~~~~~~~~~~
30+
!!! error TS2322: Type '(v: number) => number' is not assignable to type '(x: number) => string'.
31+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
32+
function f3({ ["show"]: showRename = v => v }: Show) {}
33+
~~~~~~~~~~
34+
!!! error TS2322: Type '(v: number) => number' is not assignable to type '(x: number) => string'.
2335
!!! error TS2322: Type 'number' is not assignable to type 'string'.
2436

2537
interface Nested {

tests/baselines/reference/contextuallyTypedBindingInitializerNegative.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ interface Show {
33
show: (x: number) => string;
44
}
55
function f({ show: showRename = v => v }: Show) {}
6+
function f2({ "show": showRename = v => v }: Show) {}
7+
function f3({ ["show"]: showRename = v => v }: Show) {}
68

79
interface Nested {
810
nested: Show
@@ -29,6 +31,12 @@ function h({ prop = "baz" }: StringUnion) {}
2931
function f(_a) {
3032
var _b = _a.show, showRename = _b === void 0 ? function (v) { return v; } : _b;
3133
}
34+
function f2(_a) {
35+
var _b = _a["show"], showRename = _b === void 0 ? function (v) { return v; } : _b;
36+
}
37+
function f3(_a) {
38+
var _b = "show", _c = _a[_b], showRename = _c === void 0 ? function (v) { return v; } : _c;
39+
}
3240
function ff(_a) {
3341
var _b = _a.nested, nestedRename = _b === void 0 ? { show: function (v) { return v; } } : _b;
3442
}

tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ interface Show {
33
show: (x: number) => string;
44
}
55
function f({ show = v => v.toString() }: Show) {}
6+
function f2({ "show": showRename = v => v.toString() }: Show) {}
7+
function f3({ ["show"]: showRename = v => v.toString() }: Show) {}
68

79
interface Nested {
810
nested: Show
@@ -23,3 +25,5 @@ interface StringIdentity {
2325
stringIdentity(s: string): string;
2426
}
2527
let { stringIdentity: id = arg => arg }: StringIdentity = { stringIdentity: x => x};
28+
29+

tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ interface Show {
33
show: (x: number) => string;
44
}
55
function f({ show: showRename = v => v }: Show) {}
6+
function f2({ "show": showRename = v => v }: Show) {}
7+
function f3({ ["show"]: showRename = v => v }: Show) {}
68

79
interface Nested {
810
nested: Show

0 commit comments

Comments
 (0)