typescript将字符串数组转换为类型 Posted on 2019-06-03 123456function asLiterals<T extends string>(arr: T[]): T[] { return arr; }const s = asLiterals(['foo', 'rolo', 'zoombaz']);// s => ('foo' | 'rolo' | 'zoombaz')[]type v = {[K in (typeof s)[number]]: string};// v => { foo: string, role: string, zoombaz: string }