-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[Feature request] Support for type checking properties in an object with different types #23927
Comments
Possibly related: #22509 |
Thanks, and another similar one too, #17867 |
Which means, currently, there is no optimal way to describe an object as the one I mentioned in the post like I did in type ComponentState {
isFormOK: boolean;
[fieldName: string]: FieldState
} |
Removing the Honestly, the cleanest approach is just to adjust your data structure to the needs you have: export interface ComponentState {
isFormOK: boolean;
fields: { [fieldName: string]: FieldState };
} Now you represent your data structure type-safe and clean. You don't mix the actual fields with your status information anymore. |
Flow might let you write that, but it's clearly unsound for computed properties. If you were to support that in TS, then computed properties should probably be the intersection for set, so const state: ComponentState;
let prop: string;
state[prop] = ... // must be of type boolean & FieldState
const result: boolean | FieldState = state[prop] // we get the union back Although this brings in usability issues because there is no way to describe a string that is not const state: ComponentState;
let prop: string;
if(prop === "isFormOk") {
state[prop] = true // ok as we've narrowed prop
} else {
state[prop] = ...;
// prop is still string here, without the knowledge
// that it cannot be isFormOk. We are forced to use
// the intersection (and consequently a cast).
} |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I got an object like this:
There could be many fields which have the same structure as the above
fieldA
property, and in the code, I will actually use index property to access them because they are random. So I declared types like this:If you just swap those
interface
withtype
, then it works forflow.js
. Whereas can't do that in Typescript, because as the doc said:And the error is:
Property isFormOK of type boolean is not assignable to string index type FieldState
I think it's fine, because when I access the state using an index, I might go to the case where the index equals isFormOK but I still want to use it like a FieldState. I know. But how could I solve it and type check this object?
Currently, we have 2 options:
Tried changing
[fieldName: string]: FieldState
to[fieldName: string]: {}
, then cast it toFieldState
everytime when use it. it works. But then the type for state doesn't convey the message where the real structure of this object.Tried changing
ComponentState
toI currently use the 2nd approach.
What about just something like:
Then the
[fieldName: string]
will excludeisFormOK
when type checking the related instance.Because we need some syntax to describe the actural structure of an object, so maybe reusing the existing syntax but expanding its underneath check will support it easily. Just like
flow.js
.The text was updated successfully, but these errors were encountered: