面试题答案
一键面试type ComplexType =
{ kind: 'A', value: number } |
{ kind: 'B', value: string } |
{ kind: 'C', value: boolean };
function handleComplexType(data: ComplexType) {
if (data.kind === 'A') {
const result = data.value + 1;
console.log(`处理类型A,结果: ${result}`);
} else if (data.kind === 'B') {
const result = data.value + '拼接的字符串';
console.log(`处理类型B,结果: ${result}`);
} else if (data.kind === 'C') {
const result = data.value ? '真值' : '假值';
console.log(`处理类型C,结果: ${result}`);
}
}