在开关案例

时间:2017-04-13 17:37:07

标签: flowtype

我试图弄清楚为什么getLabelForType有效,但getOtherLableForType输入失败:

export const things = {
    THING1: 'THING1',
    THING2: 'THING2',
}
export type Thing = $Keys<typeof things>

type Empty = 'empty type' & 'nothing there'

export default function unexpectedCase(impossible: Empty): void {
    throw new Error(`Unexpected case ${impossible}`)
}

export function getLabelForType(thing: Thing): string {
    switch (thing) {
        case 'THING1':
            return 'Thing 1'
        case 'THING2':
            return 'Thing 2'
        default:
            unexpectedCase(thing)
            return ''
    }
}


export function getOtherLabelForType(thing: Thing): string {
    (things.THING1: Thing)
    switch (thing) {
        case things.THING1:
            return 'Thing 1'
        case things.THING1:
            return 'Thing 2'
        default:
            unexpectedCase(thing)
            return ''
    }
}

请参阅tryflow working example

0 个答案:

没有答案