-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
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 |
@anru but this example doesn't work, I just pasted it in here: I would love it to work, though. |
this is a workaround: #2221 (comment) |
Another workaround for the broken indexer in an exact type 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 |
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 |}; |
This doesn't make any sense, it should be an error:
The text was updated successfully, but these errors were encountered: