Flow: Exact object types shouldn't allow array access without an indexer annotation

Created on 27 Jun 2017  路  5Comments  路  Source: facebook/flow

Flow passes the following cleanly, but it shouldn't:

type T = {||};
declare var k: T;
k[1] = 7;

Similar to https://github.com/facebook/flow/issues/3162.

object model

Most helpful comment

I'd expect even a _non_-exact object type to fail with the "access of computed property/element . Indexable signature not found" error. The following should be a type error:

type Foo = { n: number };
const foo: Foo = {n: 0};
const name: string = 'n';
const prop: boolean = foo[name];

I want computed property access to be allowed only when there's an indexer property.

All 5 comments

I'd expect even a _non_-exact object type to fail with the "access of computed property/element . Indexable signature not found" error. The following should be a type error:

type Foo = { n: number };
const foo: Foo = {n: 0};
const name: string = 'n';
const prop: boolean = foo[name];

I want computed property access to be allowed only when there's an indexer property.

@jfirebaugh if you remove the string annotation from name it will give you your flow error.

type Foo = { n: number };
const foo: Foo = {n: 0};
const name = 'n';
const prop: boolean = foo[name];

Interesting -- I suppose in that case it gets typed as a literal, so essentially equivalent to foo['n'] which is also an error.

In the context where I encountered the issue, that's not an option however; name is not assigned from a string literal, and not explicitly annotated -- flow infers a string type.

Well, I believe this is the source of your problem. You can again work around this by changing your type definition:

type Foo = { n: number, [any]: mixed };

Bump.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cubika picture cubika  路  3Comments

iamchenxin picture iamchenxin  路  3Comments

jamiebuilds picture jamiebuilds  路  3Comments

bennoleslie picture bennoleslie  路  3Comments

ghost picture ghost  路  3Comments