使用要点
- 演示输出是带标签的(
dark:、beta:、legacy:),每个操作都会留下自己的一行标注。
常见问题
复制和运行之间有安装步骤吗?
没有。从复制到运行之间没有别的要做。
核心语句
const hasLegacy: Includes<Flags, 'legacy'> = false;
代码示例
type Includes<T extends readonly unknown[], Value> = T extends readonly [
infer Head,
...infer Tail extends readonly unknown[],
]
? [Head] extends [Value]
? true
: Includes<Tail, Value>
: false;
type Flags = ['dark', 'compact', 'beta'];
const hasDark: Includes<Flags, 'dark'> = true;
const hasBeta: Includes<Flags, 'beta'> = true;
const hasLegacy: Includes<Flags, 'legacy'> = false;
console.log('dark:', hasDark);
console.log('beta:', hasBeta);
console.log('legacy:', hasLegacy);
// 字符串字面量区分大小写,'Dark' 不是成员
type HasUpper = Includes<Flags, 'Dark'>;
const upper: HasUpper = false;
console.log('uppercase:', upper);
// 联合值只要有一个成员匹配就返回 true
type HasWide = Includes<Flags, 'beta' | 'legacy'>;
const wide: HasWide = true;
console.log('wide match:', wide);