场景如下:
type Option = 'a' | 'b' | 'c' | 'd'
type Question = {
message: string;
options: Option[];
default: Option // here's the issue
}
我希望default
props 是question.options
内使用的选项之一.例如:
const q1: Question = {
message: 'first question',
options: ['a', 'b'],
default: 'a'
}
const q2: Question = {
message: 'second question',
options: ['c', 'd'],
default: 'a' // I want this to give an error because 'a' is not in 'c' | 'd'
}
我怎样才能做到这一点呢?