#How to ensure that type if never?

2 messages · Page 1 of 1 (latest)

amber dragon
#

I have case where the obj.type can have a set of values, and I check the values in if statments and return if it matches.

type obj = {type: 'typeA' | 'typeB'}

if(obj.type === 'typeA') return obj
if(obj.type === 'typeB') return obj

// At this point typescript know that obj.type is never
// so I want to write a code here that will have type error
// if the obj.type is not never, so I won't forget adding checks
// for all values (if I add more later).
#

Chat GPT helped
this line will show error if I miss some check

const exhaustiveCheck: never = notificationType

!resolved