Literal Types
Using literal values as types
Flow has primitive types for literal values, but can also use literal values as types.
For example, instead of accepting number
type, we could accept only the
literal value 2
.
1 2 3 4 5 6 7 8 9 10 |
|
{"value":"// @flow\nfunction acceptsTwo(value: 2) {\n // ...\n}\n\nacceptsTwo(2); // Works!\n// $ExpectError\nacceptsTwo(3); // Error!\n// $ExpectError\nacceptsTwo(\"2\"); // Error!\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_FUNCTION","context":"normal","value":"function","line":2,"start":9,"end":17},{"type":"T_IDENTIFIER","context":"normal","value":"acceptsTwo","line":2,"start":18,"end":28},{"type":"T_LPAREN","context":"normal","value":"(","line":2,"start":28,"end":29},{"type":"T_IDENTIFIER","context":"normal","value":"value","line":2,"start":29,"end":34},{"type":"T_COLON","context":"type","value":":","line":2,"start":34,"end":35},{"type":"T_NUMBER_SINGLETON_TYPE","context":"type","value":"2","line":2,"start":36,"end":37},{"type":"T_RPAREN","context":"normal","value":")","line":2,"start":37,"end":38},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":39,"end":40},{"type":"Line","context":"comment","value":"// ...","line":3,"start":43,"end":49},{"type":"T_RCURLY","context":"normal","value":"}","line":4,"start":50,"end":51},{"type":"T_IDENTIFIER","context":"normal","value":"acceptsTwo","line":6,"start":53,"end":63},{"type":"T_LPAREN","context":"normal","value":"(","line":6,"start":63,"end":64},{"type":"T_NUMBER","context":"normal","value":"2","line":6,"start":64,"end":65},{"type":"T_RPAREN","context":"normal","value":")","line":6,"start":65,"end":66},{"type":"T_SEMICOLON","context":"normal","value":";","line":6,"start":66,"end":67},{"type":"Line","context":"comment","value":"// Works!","line":6,"start":70,"end":79},{"type":"Line","context":"comment","value":"// $ExpectError","line":7,"start":80,"end":95},{"type":"T_IDENTIFIER","context":"normal","value":"acceptsTwo","line":8,"start":96,"end":106},{"type":"T_LPAREN","context":"normal","value":"(","line":8,"start":106,"end":107},{"type":"T_NUMBER","context":"normal","value":"3","line":8,"start":107,"end":108},{"type":"T_RPAREN","context":"normal","value":")","line":8,"start":108,"end":109},{"type":"T_SEMICOLON","context":"normal","value":";","line":8,"start":109,"end":110},{"type":"Line","context":"comment","value":"// Error!","line":8,"start":113,"end":122},{"type":"Line","context":"comment","value":"// $ExpectError","line":9,"start":123,"end":138},{"type":"T_IDENTIFIER","context":"normal","value":"acceptsTwo","line":10,"start":139,"end":149},{"type":"T_LPAREN","context":"normal","value":"(","line":10,"start":149,"end":150},{"type":"T_STRING","context":"normal","value":"\"2\"","line":10,"start":150,"end":153},{"type":"T_RPAREN","context":"normal","value":")","line":10,"start":153,"end":154},{"type":"T_SEMICOLON","context":"normal","value":";","line":10,"start":154,"end":155},{"type":"Line","context":"comment","value":"// Error!","line":10,"start":156,"end":165}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot call `acceptsTwo` with `3` bound to `value` because number [1] is incompatible with number literal `2` [2]. [incompatible-call]","context":"acceptsTwo(3); // Error!","source":"-","start":{"line":8,"column":12,"offset":107},"end":{"line":8,"column":12,"offset":108}}],"operation":null},{"id":"E2","messages":[{"id":"E2M1","description":"Cannot call `acceptsTwo` with `\"2\"` bound to `value` because string [1] is incompatible with number literal `2` [2]. [incompatible-call]","context":"acceptsTwo(\"2\"); // Error!","source":"-","start":{"line":10,"column":12,"offset":150},"end":{"line":10,"column":14,"offset":153}}],"operation":null}]}
You can use primitive values for these types:
- Booleans: like
true
orfalse
- Numbers: like
42
or3.14
- Strings: like
"foo"
or"bar"
Using these with union types is powerful:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
{"value":"// @flow\nfunction getColor(name: \"success\" | \"warning\" | \"danger\") {\n switch (name) {\n case \"success\" : return \"green\";\n case \"warning\" : return \"yellow\";\n case \"danger\" : return \"red\";\n }\n}\n\ngetColor(\"success\"); // Works!\ngetColor(\"danger\"); // Works!\n// $ExpectError\ngetColor(\"error\"); // Error!\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_FUNCTION","context":"normal","value":"function","line":2,"start":9,"end":17},{"type":"T_IDENTIFIER","context":"normal","value":"getColor","line":2,"start":18,"end":26},{"type":"T_LPAREN","context":"normal","value":"(","line":2,"start":26,"end":27},{"type":"T_IDENTIFIER","context":"normal","value":"name","line":2,"start":27,"end":31},{"type":"T_COLON","context":"type","value":":","line":2,"start":31,"end":32},{"type":"T_STRING","context":"type","value":"\"success\"","line":2,"start":33,"end":42},{"type":"T_BIT_OR","context":"type","value":"|","line":2,"start":43,"end":44},{"type":"T_STRING","context":"type","value":"\"warning\"","line":2,"start":45,"end":54},{"type":"T_BIT_OR","context":"type","value":"|","line":2,"start":55,"end":56},{"type":"T_STRING","context":"type","value":"\"danger\"","line":2,"start":57,"end":65},{"type":"T_RPAREN","context":"normal","value":")","line":2,"start":65,"end":66},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":67,"end":68},{"type":"T_SWITCH","context":"normal","value":"switch","line":3,"start":71,"end":77},{"type":"T_LPAREN","context":"normal","value":"(","line":3,"start":78,"end":79},{"type":"T_IDENTIFIER","context":"normal","value":"name","line":3,"start":79,"end":83},{"type":"T_RPAREN","context":"normal","value":")","line":3,"start":83,"end":84},{"type":"T_LCURLY","context":"normal","value":"{","line":3,"start":85,"end":86},{"type":"T_CASE","context":"normal","value":"case","line":4,"start":91,"end":95},{"type":"T_STRING","context":"normal","value":"\"success\"","line":4,"start":96,"end":105},{"type":"T_COLON","context":"normal","value":":","line":4,"start":106,"end":107},{"type":"T_RETURN","context":"normal","value":"return","line":4,"start":108,"end":114},{"type":"T_STRING","context":"normal","value":"\"green\"","line":4,"start":115,"end":122},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":122,"end":123},{"type":"T_CASE","context":"normal","value":"case","line":5,"start":128,"end":132},{"type":"T_STRING","context":"normal","value":"\"warning\"","line":5,"start":133,"end":142},{"type":"T_COLON","context":"normal","value":":","line":5,"start":143,"end":144},{"type":"T_RETURN","context":"normal","value":"return","line":5,"start":145,"end":151},{"type":"T_STRING","context":"normal","value":"\"yellow\"","line":5,"start":152,"end":160},{"type":"T_SEMICOLON","context":"normal","value":";","line":5,"start":160,"end":161},{"type":"T_CASE","context":"normal","value":"case","line":6,"start":166,"end":170},{"type":"T_STRING","context":"normal","value":"\"danger\"","line":6,"start":171,"end":179},{"type":"T_COLON","context":"normal","value":":","line":6,"start":181,"end":182},{"type":"T_RETURN","context":"normal","value":"return","line":6,"start":183,"end":189},{"type":"T_STRING","context":"normal","value":"\"red\"","line":6,"start":190,"end":195},{"type":"T_SEMICOLON","context":"normal","value":";","line":6,"start":195,"end":196},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":199,"end":200},{"type":"T_RCURLY","context":"normal","value":"}","line":8,"start":201,"end":202},{"type":"T_IDENTIFIER","context":"normal","value":"getColor","line":10,"start":204,"end":212},{"type":"T_LPAREN","context":"normal","value":"(","line":10,"start":212,"end":213},{"type":"T_STRING","context":"normal","value":"\"success\"","line":10,"start":213,"end":222},{"type":"T_RPAREN","context":"normal","value":")","line":10,"start":222,"end":223},{"type":"T_SEMICOLON","context":"normal","value":";","line":10,"start":223,"end":224},{"type":"Line","context":"comment","value":"// Works!","line":10,"start":225,"end":234},{"type":"T_IDENTIFIER","context":"normal","value":"getColor","line":11,"start":235,"end":243},{"type":"T_LPAREN","context":"normal","value":"(","line":11,"start":243,"end":244},{"type":"T_STRING","context":"normal","value":"\"danger\"","line":11,"start":244,"end":252},{"type":"T_RPAREN","context":"normal","value":")","line":11,"start":252,"end":253},{"type":"T_SEMICOLON","context":"normal","value":";","line":11,"start":253,"end":254},{"type":"Line","context":"comment","value":"// Works!","line":11,"start":256,"end":265},{"type":"Line","context":"comment","value":"// $ExpectError","line":12,"start":266,"end":281},{"type":"T_IDENTIFIER","context":"normal","value":"getColor","line":13,"start":282,"end":290},{"type":"T_LPAREN","context":"normal","value":"(","line":13,"start":290,"end":291},{"type":"T_STRING","context":"normal","value":"\"error\"","line":13,"start":291,"end":298},{"type":"T_RPAREN","context":"normal","value":")","line":13,"start":298,"end":299},{"type":"T_SEMICOLON","context":"normal","value":";","line":13,"start":299,"end":300},{"type":"Line","context":"comment","value":"// Error!","line":13,"start":303,"end":312}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot call `getColor` with `\"error\"` bound to `name` because string [1] is incompatible with literal union [2]. [incompatible-call]","context":"getColor(\"error\"); // Error!","source":"-","start":{"line":13,"column":10,"offset":291},"end":{"line":13,"column":16,"offset":298}}],"operation":null}]}
Was this guide helpful? Let us know by sending a message to @flowtype.