Skip to content

When an object key has annotated type, the key and value are unchecked #6568

@wchargin

Description

@wchargin

The following code should obviously have a type error, but does not:

// @flow
const key: string = "magic";
const o: {[string]: number} = {[key]: "I am not a number"};  // no type error!!!
o.magic.toFixed();  // runtime error

Some more examples:

// @flow
const annotated: string = "hi";
({[annotated]: "I am not a number"}: {[string]: number});  // no error!!!
({[annotated]: "I am not a number"}: {|[string]: number|});  // even when exact
({[annotated]: "I am not a number"}: {+[string]: number});  // or covariant
({[annotated]: "I am not a number"}: {|+[string]: number|});  // or both

const inferred = "hi";
({[inferred]: "I am not a number"}: {[string]: number});  // properly fails

Edit: It appears that the key is not checked, either:

// @flow
type Fruit = "APPLE" | "ORANGE";
const properlyFails1: {[Fruit]: number} = {BRICK: 123};
const properlyFails2: {[Fruit]: number} = {["BRICK"]: 234};
const wat3: {[Fruit]: number} = {[("BRICK": string)]: 345};  // no error!!!
const wat4: {[Fruit]: number} = {[("BRICK": string)]: "wat"};  // no error!!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typing: soundnessNo false negatives (type checker claims that there is no error in the incorrect program)bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions