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
Open

Exact object types shouldn't allow indexer properties #3162

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

Comments

@jamiebuilds
Copy link
Contributor

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

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

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
Copy link

@anru but this example doesn't work, I just pasted it in here:

https://flow.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AQWnPFgCYDSWaAzqALygDksAdlvaAD4Mryz3LrZQAWTQB5AEYArWqAAkAUQAeAQwDGKADwBvRKFABtANYUAXHgIIS5KgF0zAWwCWikogC+APmSrmlFKGUzEQlpOh09ZiwzAEYAGl1QblgzAGZ3IA

I would love it to work, though.

@mindriven
Copy link

mindriven commented Mar 12, 2018

this is a workaround: #2221 (comment)

@peter-leonov
Copy link
Contributor

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
Copy link

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
Development

No branches or pull requests

7 participants