2017-01-09 16 views
0

所以我有這個數據結構在打字稿,你怎麼了,你鍵入特定的鍵對象常量

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 };'。 –

回答

1

感謝傑夫,即做到了:

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