Skip to content

Add index signature for anonymous object literal type #37903

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 5 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13660,6 +13660,19 @@ namespace ts {
return anyType;
}
if (accessExpression && !isConstEnumObjectType(objectType)) {
if (isObjectLiteralType(objectType)) {
if (noImplicitAny && indexType.flags & (TypeFlags.StringLiteral | TypeFlags.NumberLiteral)) {
diagnostics.add(createDiagnosticForNode(accessExpression, Diagnostics.Property_0_does_not_exist_on_type_1, (indexType as StringLiteralType).value, typeToString(objectType)));
return undefinedType;
}
else if (indexType.flags & (TypeFlags.Number | TypeFlags.String)) {
const types = map((<ResolvedType>objectType).properties, property => {
return getTypeOfSymbol(property);
});
return getUnionType(append(types, undefinedType));
}
}

if (objectType.symbol === globalThisSymbol && propName !== undefined && globalThisSymbol.exports!.has(propName) && (globalThisSymbol.exports!.get(propName)!.flags & SymbolFlags.BlockScoped)) {
error(accessExpression, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
tests/cases/compiler/indexedAccessWithFreshObjectLiteral.ts(34,10): error TS2339: Property 'z' does not exist on type '{ a: number; b: string; c: boolean; }'.


==== tests/cases/compiler/indexedAccessWithFreshObjectLiteral.ts (1 errors) ====
function foo (id: string) {
return {
a: 1,
b: "",
c: true
}[id]
}

function bar (id: 'a' | 'b') {
return {
a: 1,
b: "",
c: false
}[id]
}

function baz (id: '1' | '2') {
return {
1: 1,
2: "",
3: false
}[id]
}

function qux (id: 1 | 2) {
return {
1: 1,
2: "",
3: false
}[id]
}

function quux (id: 'a' | 'b' | 'z') {
return {
~
a: 1,
~~~~~~~~~~~
b: "",
~~~~~~~~~~~~
c: false
~~~~~~~~~~~~~~
}[id]
~~~~~~~
!!! error TS2339: Property 'z' does not exist on type '{ a: number; b: string; c: boolean; }'.
}

function corge(id: string) {
return ({
a: 123,
b: ""
} as Record<string, number | string>)[id]
}

function grault(id: string) {
return ({
a: 123,
b: ""
} as { [k: string]: string | number})[id]
}

105 changes: 105 additions & 0 deletions tests/baselines/reference/indexedAccessWithFreshObjectLiteral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//// [indexedAccessWithFreshObjectLiteral.ts]
function foo (id: string) {
return {
a: 1,
b: "",
c: true
}[id]
}

function bar (id: 'a' | 'b') {
return {
a: 1,
b: "",
c: false
}[id]
}

function baz (id: '1' | '2') {
return {
1: 1,
2: "",
3: false
}[id]
}

function qux (id: 1 | 2) {
return {
1: 1,
2: "",
3: false
}[id]
}

function quux (id: 'a' | 'b' | 'z') {
return {
a: 1,
b: "",
c: false
}[id]
}

function corge(id: string) {
return ({
a: 123,
b: ""
} as Record<string, number | string>)[id]
}

function grault(id: string) {
return ({
a: 123,
b: ""
} as { [k: string]: string | number})[id]
}


//// [indexedAccessWithFreshObjectLiteral.js]
"use strict";
function foo(id) {
return {
a: 1,
b: "",
c: true
}[id];
}
function bar(id) {
return {
a: 1,
b: "",
c: false
}[id];
}
function baz(id) {
return {
1: 1,
2: "",
3: false
}[id];
}
function qux(id) {
return {
1: 1,
2: "",
3: false
}[id];
}
function quux(id) {
return {
a: 1,
b: "",
c: false
}[id];
}
function corge(id) {
return {
a: 123,
b: ""
}[id];
}
function grault(id) {
return {
a: 123,
b: ""
}[id];
}
123 changes: 123 additions & 0 deletions tests/baselines/reference/indexedAccessWithFreshObjectLiteral.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
=== tests/cases/compiler/indexedAccessWithFreshObjectLiteral.ts ===
function foo (id: string) {
>foo : Symbol(foo, Decl(indexedAccessWithFreshObjectLiteral.ts, 0, 0))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 0, 14))

return {
a: 1,
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 1, 10))

b: "",
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 2, 11))

c: true
>c : Symbol(c, Decl(indexedAccessWithFreshObjectLiteral.ts, 3, 12))

}[id]
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 0, 14))
}

function bar (id: 'a' | 'b') {
>bar : Symbol(bar, Decl(indexedAccessWithFreshObjectLiteral.ts, 6, 1))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 8, 14))

return {
a: 1,
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 9, 10))

b: "",
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 10, 11))

c: false
>c : Symbol(c, Decl(indexedAccessWithFreshObjectLiteral.ts, 11, 12))

}[id]
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 8, 14))
}

function baz (id: '1' | '2') {
>baz : Symbol(baz, Decl(indexedAccessWithFreshObjectLiteral.ts, 14, 1))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 16, 14))

return {
1: 1,
>1 : Symbol(1, Decl(indexedAccessWithFreshObjectLiteral.ts, 17, 10))

2: "",
>2 : Symbol(2, Decl(indexedAccessWithFreshObjectLiteral.ts, 18, 11))

3: false
>3 : Symbol(3, Decl(indexedAccessWithFreshObjectLiteral.ts, 19, 12))

}[id]
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 16, 14))
}

function qux (id: 1 | 2) {
>qux : Symbol(qux, Decl(indexedAccessWithFreshObjectLiteral.ts, 22, 1))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 24, 14))

return {
1: 1,
>1 : Symbol(1, Decl(indexedAccessWithFreshObjectLiteral.ts, 25, 10))

2: "",
>2 : Symbol(2, Decl(indexedAccessWithFreshObjectLiteral.ts, 26, 11))

3: false
>3 : Symbol(3, Decl(indexedAccessWithFreshObjectLiteral.ts, 27, 12))

}[id]
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 24, 14))
}

function quux (id: 'a' | 'b' | 'z') {
>quux : Symbol(quux, Decl(indexedAccessWithFreshObjectLiteral.ts, 30, 1))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 32, 15))

return {
a: 1,
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 33, 10))

b: "",
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 34, 11))

c: false
>c : Symbol(c, Decl(indexedAccessWithFreshObjectLiteral.ts, 35, 12))

}[id]
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 32, 15))
}

function corge(id: string) {
>corge : Symbol(corge, Decl(indexedAccessWithFreshObjectLiteral.ts, 38, 1))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 40, 15))

return ({
a: 123,
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 41, 11))

b: ""
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 42, 13))

} as Record<string, number | string>)[id]
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 40, 15))
}

function grault(id: string) {
>grault : Symbol(grault, Decl(indexedAccessWithFreshObjectLiteral.ts, 45, 1))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 47, 16))

return ({
a: 123,
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 48, 11))

b: ""
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 49, 13))

} as { [k: string]: string | number})[id]
>k : Symbol(k, Decl(indexedAccessWithFreshObjectLiteral.ts, 51, 10))
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 47, 16))
}

Loading