Last active
March 25, 2017 13:11
-
-
Save Ibro/e23ffe8d7e4f5ca0a4501a970589f7fe to your computer and use it in GitHub Desktop.
TypeScript tuple types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tupleType: [string, number]; | |
tupleType = ['hey', 13]; | |
// tupleType = [5, 'hey']; // Error - '[number, string]' is not assignable to '[string, number]' | |
// tupleType = ['hey', 'tuple']; // Error - '[string, string]' is not assignable to '[string, number]' | |
tupleType[3] = 54; // this works well because union type string | number is used for indices outside of known ones | |
tupleType[4] = 'test'; // works well because it can be string or number, union type | |
// tupleType[5] = true; // Error - Type 'true' is not assignable to type 'string | number' | |
console.log(tupleType); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment