Skip to content

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

Closed
normalser opened this issue Nov 23, 2016 · 4 comments
Closed

CFA issue #12457

normalser opened this issue Nov 23, 2016 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@normalser
Copy link

normalser commented Nov 23, 2016

It's probably duplicate but it's not easy to find so apologies if it is

interface A {
    a: string // or `a: 'a'`
}

interface B {
    a: number
}

declare var c:A|B

if (typeof c.a == 'string') { // or `if (c.a == 'a')`
    c // <- Type: A|B, should be A ?
}
else {
    c // <- Type: A|B, should be B ?
}

// where this seems ok:

declare var d:string|number // or `'a'|number`

if (typeof d == 'string') { // or `if (d == 'a')`
    d // Type: string           // Type: 'a'
}
else {
    d // Type: number
}
@HolgerJeromin
Copy link
Contributor

Adding a User-Defined Type Guards should solve your problem.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Nov 28, 2016
@RyanCavanaugh
Copy link
Member

I'd be interested to see what the un-reduced version of this code looked like

@normalser
Copy link
Author

@RyanCavanaugh I updated above with comments cause I initially experienced it with string vs number but played with some other cases like string literal types vs number

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

@RyanCavanaugh
Copy link
Member

Closing due to minimal feedback; people don't seem to be encountering this very often

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants