最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Type not narrowing through guard and type check - Stack Overflow

programmeradmin5浏览0评论

Suppose that you have the following types:

type Foo = {
    value: string | number
}

type Extra<F> = F extends Foo 
   ? F['value'] extends number 
      ? F & {extra: number}
      : F & {extra: boolean}
   : F

and the next piece of code:

const isExtra = (f: Foo): f is Extra<Foo> => 'extra' in f

const process = <T extends Foo>(item: T) => {
    if (isExtra(item)) {
        if (typeof item.value === 'number') {
            const result = item.extra // boolean !!!
        }
    }
}

const result is resolving to boolean while it should resolve to number (according to types). What is wrong?

Playground

发布评论

评论列表(0)

  1. 暂无评论