意味

2017-07-14 15 views
0

私は、次のいずれかのよう、インタフェース定義に遭遇しました:意味

​​

プロパティ定義の意味は何ですか?どこでオンラインで説明できますか?
left/center/rightは、assignにのみ割り当てることができますか?

答えて

1

正確には、それはどのように動作するのですか。 Advanced Typesに移動し、「文字列リテラルタイプ」までスクロールします。

基本的に単純な列挙型で、特定の文字列値のみを許可し、 "マジックストリング"で動作を受け入れるライブラリに注釈を付けるのに非常に便利です。

記事を引用:

文字列リテラルの種類は、あなたが文字列 を持っている必要があります正確な値を指定することができます。実際には、文字列のリテラル型は、 型、型ガード、および型エイリアスとうまく組み合わせます。これらの機能を使用して、文字列で列挙型の動作を得ることができます。 正しいです

type Easing = "ease-in" | "ease-out" | "ease-in-out"; 
class UIElement { 
    animate(dx: number, dy: number, easing: Easing) { 
     if (easing === "ease-in") { 
      // ... 
     } 
     else if (easing === "ease-out") { 
     } 
     else if (easing === "ease-in-out") { 
     } 
     else { 
      // error! should not pass null or undefined. 
     } 
    } 
} 

let button = new UIElement(); 
button.animate(0, 0, "ease-in"); 
button.animate(0, 0, "uneasy"); // error: "uneasy" is not allowed here 
0

。これは、alignプロパティをこれらの3つの値の1つに限定しているため、効果的に列挙型のように機能します。

interface config { 
    align?: 'left' | 'center' | 'right'; 
} 

// this is valid 
const a: config = { 
    align:'left' 
} 

// this is not 
const b: config = { 
    align:'down' 
}