-
Notifications
You must be signed in to change notification settings - Fork 12.8k
CFA issue #12457
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
Comments
Adding a User-Defined Type Guards should solve your problem. |
I'd be interested to see what the un-reduced version of this code looked like |
@RyanCavanaugh I updated above with comments cause I initially experienced it with Here is the un-reduced but still simplified example of the real code: import * as React from "react";
interface StringField {
data: string
maxLength: number
}
interface NumberField {
data: number
maxValue: number
}
type FormField = StringField | NumberField
interface Props {
fields: FormField[]
}
export class FormComponent extends React.Component<Props, {}> {
render() {
const { fields } = this.props
return (
<div>
{fields.map(field=>
<div>
{typeof(field.data) == 'string' && <div>TODO: Render component for string: {field.data} with max length: {field.maxLength}</div>}
// Error here because field's type is still StringField | NumberField
{typeof(field.data) == 'number' && <div>TODO: Render field for number: {field.data} with max value: {field.maxValue}</div>}
// Error here because field's type is still StringField | NumberField
</div>)}
</div>
);
}
} I know there are workarounds - but would be fantastic if TS could handle such cases as well. Thanks |
Closing due to minimal feedback; people don't seem to be encountering this very often |
It's probably duplicate but it's not easy to find so apologies if it is
The text was updated successfully, but these errors were encountered: