waltcow / blog

A personal blog
21 stars 2 forks source link

使用Typescript的用法备忘 #42

Open waltcow opened 5 years ago

waltcow commented 5 years ago
  1. Record 可以保证映射完整:
export enum EnvironmentType {
    development = "development",
    staging = "staging",
    production = "production"
}

export interface EnvironmentConfig {
    server: string
    port: number
}

type EnvironmentConfigMap = Record<EnvironmentType, EnvironmentConfig>
  1. infer
type FlattenIfArray<T> = T extends (infer R)[] ? R : T

Check if our generic Type is the array, If it is array extract the real type from it, If it does not leave it as is