F (Index signature)
title: Typescript
date: 2019-08-19 tag: Typescript
description: Typescript (Index signature)
const VERSIONS = { v1: 'V1', v2: 'V2',
}
typeof
type VERSIONSType = typeof VERSIONS;
// type VERSIONSType = {
v1: string v2: string }
Object.keys(VERSIONS).map(v => ({ value: v, title: VERSIONS[v]
})) // Throw Error // => No index signature with a parameter
indexSignature
export interface IndexSignature { [key: string]: any;
}
Object.keys(VERSIONS).map(v => ({ value: v, title: (VERSIONS as IndexSignature)[v]
}))
any key key
keyof key union
type VERSIONSKey = keyof typeof VERSIONS; (Object.keys(VERSIONS) as VERSIONSKey[]).map(v => ({
value: v, title: VERSIONS[v] }))
() => { const VERSIONS = { v1: 'V1', v2: 'V2', } interface IndexSignature { [key: string]: any; } type VERSIONSKey = keyof typeof VERSIONS; return ( Bad {Object.keys(VERSIONS).map(v => ({ value: v, title: (VERSIONS as IndexSignature)[v] }))} Good (Object.keys(VERSIONS) as VERSIONSKey[]).map(v => ({ value: v, title: VERSIONS[v] })) )
}
Typescript set map
set map
// set type Size = 'sm' | 'md' | 'lg'; // map interface Ob {
a: string b: number }
type ObKeys = keyof Ob; // 'a' | 'b' type ObVals = Ob[keyof Ob] // string | number
type SizeMap = {
[k in Size]: number
} //
type SizeMap = {
sm: number
md: number
lg: number
}
set map Record
type Record = { [P in K]: T }; type Size = 'sm' | 'md' | 'lg'; type SizeMap = Record // type SizeMap = { // sm: number // md: number // lg: number // }
map
Pick
type Pick = { [P in K]: T[P] }; type PickSizeMap = Pick; // type SizeMap = { // sm: number // md: number // }
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- mastering typescript 81 95 233 163 8888
- declare module typescript with default parameter functions
- typescript parse json to object
- typescript stanford university
- declare map interface type
- javascript patterns in typescript
- chapter 1 and the typescript language
- declare function in object javascript
- typescript modern javascript development
- f index signature