Skip to content

In JS, object literal with computed property has no index signature #26208

Closed
@ajafff

Description

@ajafff

TypeScript Version: 3.1.0-dev.20180803

Search Terms:

Code

// @filename: test.js
// @strict: true

/*1*/
let empty = {};
empty.foo;

/*2*/
let key = Math.random();
let computed = {[key]: 1};
computed[key];

Expected behavior:

empty has no index signature.
computed has an index signature {[x: number]: number}.
There's no implicitAny error on the element access computed[key].

Actual behavior:

None of the object literals has an index signature. This causes the element access computed[key] to have an implicitAny error.

Note that this is not an error in a TS file.

Related Issues:
This is caused by @weswigham's PR #25996. I agree with not adding an implicit string index signature to every JS object literal. But not adding one where it makes sense just loses type information.

Metadata

Metadata

Labels

BugA bug in TypeScriptcheckJsRelates to checking JavaScript using TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions