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

Exact object types shouldn't allow indexer properties #3162

Open
jamiebuilds opened this issue Jan 9, 2017 · 5 comments
Assignees

Comments

@jamiebuilds
Copy link
Contributor

@jamiebuilds jamiebuilds commented Jan 9, 2017

This doesn't make any sense, it should be an error:

{| [prop: string]: boolean |}
@anru

This comment has been minimized.

Copy link

@anru anru commented Feb 25, 2018

There are some cases where exact types together with indexer property makes sense:

For example in this case

// @flow

type AllowedKeys = 'one' | 'two'

type MyObj = $Exact<{
  [key: AllowedKeys]: mixed
}>

const a: MyObj = {
  one: 1,
  two: 3
}

In this example I want object keys exactly match AllowedKeys

@mindriven

This comment has been minimized.

@mindriven

This comment has been minimized.

Copy link

@mindriven mindriven commented Mar 12, 2018

this is a workaround: #2221 (comment)

@peter-leonov

This comment has been minimized.

Copy link
Contributor

@peter-leonov peter-leonov commented Apr 24, 2018

Another workaround for the broken indexer in an exact type {| [AllowedKeys] |}:

type MyObj<T> = {|
    one: T,
    two: T
|}

type AllowedKeys = $Keys<MyObj<*>>

const a: MyObj<*> = {
    one: 1,
  	two: 1,
}

See on Try Flow.

BTW, if anyone knows how to get this problem solved with $Call, would be cool if you share :)

@DesignByOnyx

This comment has been minimized.

Copy link

@DesignByOnyx DesignByOnyx commented Nov 6, 2018

I have another use case where we want to allow an object with only one user-defined property. This is represented in JSONSchema as this:

{
  "type": "object",
  "maxProperties": 1,
  "minProperties": 1,
  "patternProperties": {
    "^\\w+$": {
      "type": "string"
    }
  }
}

It kind of makes sense for flow to allow the above to be represented as how the OP suggests:

type SinglePropOnly = {| [string]: string |};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.