AndreGeng / AndreGeng.github.io

blog repository
1 stars 0 forks source link

Typescript踩坑笔记 #20

Open AndreGeng opened 5 years ago

AndreGeng commented 5 years ago
  1. 如何获取enum的键值
    const enum DurationUnit {
    y = 'y',
    mo = 'mo',
    w = 'w',
    d = 'd',
    h = 'h',
    m = 'm',
    s = 's',
    ms = 'ms',
    }
    export type DurationUnitKey = keyof typeof DurationUnit;

    解释见keyof Enum

  2. Argument of type 'string' is not assignable to parameter of type 'never'.
    ['1', '2'].reduce((acc, item) => {
    acc.push(item);
    return acc;
    }, [])

    空数组被推断出的type为never, 这里把空数组类型强转为string[]. [] as string[]