i am working with a code example that looks like the following
interface BaseQuestionType {
label?: string
}
export type RadioQuestionType = BaseQuestionType & {
radio: boolean
}
export type TextQuestionType = BaseQuestionType & {
text: string
}
function test(c: boolean): (RadioQuestionType | TextQuestionType){
if (c) {
return {
radio: true
}
} else {
return {
text: '11'
}
}
}
So we have a function test(c:boolean)
which takes in a boolean and returns the corresponding object.
The issue arises when using this function, causing an error
let a = test(true).radio;
In this line of code, TypeScript gives me the following error:
Property 'radio' does not exist on type 'RadioQuestionType | TextQuestionType'.
Property 'radio' does not exist on type 'TextQuestionType'.(2339)
Although logically correct given ( c is true)
, how can I convey this to TypeScript? Or is there an alternative approach to address this?
Thank you