当前位置:网站首页>TS typescript type declaration special declaration field number is handled when the key key

TS typescript type declaration special declaration field number is handled when the key key

2022-07-07 15:47:00 Sam young

Numbers are named for keys

const data :{
    [key: number]: string} = {
    
 1: 'a',
 2: 'b'
}

// data[1] = 'a'
// data[2] = 'b'

When the object key returned by the back end is uncertain

const data :{
    [key: string]: string} = {
    
 'xxx': 'a',
 'abc': 'b'
}

// data['xxx'] = 'a'
// data['abc'] = 'b'

Common types

export type StringType = NonNullable<string | null | undefined>;
export type NumberType = NonNullable<number | null | undefined>;
export type BooleanType = NonNullable<boolean | null | undefined>;
export type StringNumberType = NonNullable<string | number | null | undefined>;
/**  With number  As key Of  */
export type NumberKey<T = any> = {
     [key: number]: T }
/** string  As key Of  */
export type StringKey<T = any> = {
     [key: string]: T }
export type Option = {
     label: string; value: string }

原网站

版权声明
本文为[Sam young]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130610295331.html