2016-12-05 23 views
1

如果我創建這個流量類型:Flowtype:可能的字符串數組不能爲空?

export type List = [ 
    'some' | 
    'strings' | 
    'allowed' | 
    'to' | 
    'use' 
]; 

我不準: const myList: List = [];

這是錯誤: empty array literal: This type is incompatible with a tuple type that expects a 1st element of non-optional type string enum

我能想出的唯一的事情就是增加typeof undefined到可能的值列表。但是必須有一種更簡單的方法來讓它變成空的?

+1

這是元組的語法:https://flowtype.org/docs/arrays.html#tuples。你寫的是一個包含其中一個字符串的單元素數組的類型。 gcanti已經解釋瞭如何實現你想要的。 –

回答

7

你定義一個元素的元組,你可能想

type Element = 
    'some' | 
    'strings' | 
    'allowed' | 
    'to' | 
    'use'; 

export type List = Element[]; 
// or export type List = Array<Element>; 

const myList: List = [] 
+0

ofcourse!謝謝! –

相關問題