Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active March 25, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibro/e23ffe8d7e4f5ca0a4501a970589f7fe to your computer and use it in GitHub Desktop.
Save Ibro/e23ffe8d7e4f5ca0a4501a970589f7fe to your computer and use it in GitHub Desktop.
TypeScript tuple types
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