2016-09-26 66 views
0

在TypeScript 2.0中引入了標記的工會。要使用它們我們就來介紹在接口判別屬性,例如:使用文字類型標記的工會和標記

interface Action { 
    type: "ACTION" 
} 

但是我不能使用字符串字面類型的判別:

let actionName: "ACTION" 

interface Action { 
    type: actionName <- error: cannot find name "actionName" 
} 

我想知道如果這是一個功能或錯誤。

回答

0

你的字符串文字定義是錯誤的。 let定義了變量,而不是一個類型。它應該是:

type actionName = "ACTION" 

interface Action { 
    type: actionName; 
} 
0

的解決方案是使用下面的符號:

const ACTION: "ACTION" = "ACTION" 

interface Action { 
    type: typeof ACTION 
}