如果只有两种情况的话,可以这样写并且很简单易懂。
interface ResponseData1 {
type: '0';
content: ContentA;
}
interface ResponseData2 {
type: '1';
content: ContentB;
}
type ResponseData = ResponseData1 | ResponseData2;
const data: ResponseData = {
type: '0',
content: {
name: 'xxx',
hobby: 'xxx', // error
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…