在打字稿,你怎么了,你键入特定的键对象常量

在打字稿,你怎么了,你键入特定的键对象常量

问题描述:

所以我有这个数据结构在打字稿,你怎么了,你键入特定的键对象常量

export interface StoreData { 
    msdb: {[tableName: string]: List<StoreModel>}; 
} 

,但我想只允许(并获得IntelliSence)对我的表名特定字符串的值。 所以,我试图像下面,但它没有工作:

var tableNames:'table_campaigns' | 'table_resources' 
export interface StoreData { 
    msdb: {[tableName]: List<StoreModel>}; 
} 

也试过,没有运气

interface IMyTables { 
    'table_campaigns: string 
    'table_resources: string; 
} 

type MyTables = keyof IMyTables; 

export interface StoreData { 
    participants: { [key: number]: any }; 
    threads: { [key: number]: any }; 
    messages: { [key: number]: any }; 
    msdb: {MyTables: List<StoreModel>}; 
} 

也试过

type allMyTables = 'table_campaigns' | 'table_resources' 

export interface StoreData { 
    msdb: {[tableName: allMyTables]: List<StoreModel>}; 
} 




Thanks for reading, 

Sean 
+1

也许我误解了g但为什么不添加显式属性而不是索引? 'msdb:{[tableName:string]:List ,table_campaigns:List ,table_resources:List };'。 –

感谢杰夫,即做到了:

export interface StoreData { 
    participants: { [key: number]: any }; 
    threads: { [key: number]: any }; 
    messages: { [key: number]: any }; 
    msdb: { 
     'table_campaigns': List<StoreModel>; 
     'table_resources': List<StoreModel>; 
    }; 
}