javascript - Type guards in typescript are narrowing to never type - Stack Overflow

I am migrating to a newer version of typescript but started getting this error in type guards.In the e

I am migrating to a newer version of typescript but started getting this error in type guards.
In the else brach, its showing question is of type never rather than showing question is of type Question. When i run this in typescript v3.9.5 it works fine but in v4.5.4 it gives this error. Pasting below my code snippet. There is also a ref image of the error in vs code

export enum QuestionTypesEnum {
    TYPE1 = 'type1',
    TYPE2 = 'type2'
}

export type McqSingle = {
    hash: string
    type: QuestionTypesEnum
    answer: string;
}

export type McqMultiple = {
    hash: string
    type: QuestionTypesEnum
    answers: string[]
}

export type Question =
| McqSingle
| McqMultiple

type EmptyQuestion = { hash: string }

const isEmptyQuestion  = (question: Question | EmptyQuestion): question is EmptyQuestion => {
    return !('type' in question)
}

let question: Question | EmptyQuestion = { hash: 'saas', type: QuestionTypesEnum.TYPE1 }

if (isEmptyQuestion(question)) {

}
else {
    question.type  // <-- Typescript plains that "Property 'type' does not exist on type 'never'"
}

Playground link

The error is:

Typescript plains that "Property 'type' does not exist on type 'never'"

TS error in vs code

I am migrating to a newer version of typescript but started getting this error in type guards.
In the else brach, its showing question is of type never rather than showing question is of type Question. When i run this in typescript v3.9.5 it works fine but in v4.5.4 it gives this error. Pasting below my code snippet. There is also a ref image of the error in vs code

export enum QuestionTypesEnum {
    TYPE1 = 'type1',
    TYPE2 = 'type2'
}

export type McqSingle = {
    hash: string
    type: QuestionTypesEnum
    answer: string;
}

export type McqMultiple = {
    hash: string
    type: QuestionTypesEnum
    answers: string[]
}

export type Question =
| McqSingle
| McqMultiple

type EmptyQuestion = { hash: string }

const isEmptyQuestion  = (question: Question | EmptyQuestion): question is EmptyQuestion => {
    return !('type' in question)
}

let question: Question | EmptyQuestion = { hash: 'saas', type: QuestionTypesEnum.TYPE1 }

if (isEmptyQuestion(question)) {

}
else {
    question.type  // <-- Typescript plains that "Property 'type' does not exist on type 'never'"
}

Playground link

The error is:

Typescript plains that "Property 'type' does not exist on type 'never'"

TS error in vs code

Share Improve this question edited May 27, 2022 at 9:49 T.J. Crowder 1.1m200 gold badges2k silver badges2k bronze badges asked May 27, 2022 at 9:39 Rahul BadenkalRahul Badenkal 631 silver badge5 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

The problem is that Question is a superset of EmptyQuestion (Question instances are valid EmptyQuestion instances). As a result, your type predicate doesn't narrow the question variable at all; its type in the if branch is still Question | EmptyQuestion.

It works if you reverse the type predicate to checking for Question, since although Question is a valid EmptyQuestion, EmptyQuestion is not a valid Question:

const isQuestion  = (question: Question | EmptyQuestion): question is Question => {
    return 'type' in question;
};

// ...

if (isQuestion(question)) {
    question.type
    // ^? −−−− type is Question
} else {
    question.hash
    // ^? −−−− type is EmptyQuestion
}

Playground link

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744846946a4596896.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信