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

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

Closed
ajafff opened this issue Aug 4, 2018 · 1 comment
Closed

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

ajafff opened this issue Aug 4, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript

Comments

@ajafff
Copy link
Contributor

ajafff commented Aug 4, 2018

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.

@weswigham
Copy link
Member

Hm. We must have been disabling this in JS in favor of the fake index signature. Shouldn't be too difficult to fix - should just be a removal of a condition in checkObjectLiteral. Thanks for the report!

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Aug 6, 2018
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 6, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.1 milestone Aug 6, 2018
@DanielRosenwasser DanielRosenwasser added Salsa checkJs Relates to checking JavaScript using TypeScript labels Aug 6, 2018
ajafff added a commit to ajafff/TypeScript that referenced this issue Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants