Skip to main content

TypeScript Unlocked: Tuple

Updated 2 min readMarkdown
Part of seriesTypeScript Unlocked
TypeScript Unlocked: Tuple
馃挕
This article is a part of TypeScript Unlocked Series on my blog. Every article in the series is short, crisp and filled with examples and code snippets. If you want to learn TypeScript from scratch for absolutely FREE!!! check it out.

Tuple is TypeScript Specific thing and we don鈥檛 have tuples in JavaScript. Tuples allow us to enforce order of types in an array.

let UserTuple: [number,string,boolean]
UserTuple = [122,"Palash",true] // valid
UserTuple = [122,"Palash","true"] // not valid

We can define tuples using type as well

type UserTuple = [number,string,boolean] // using type to define tuple
let UserTuple2: UserTuple = [122,"Palash",true] // valid
let UserTuple3: UserTuple = [122,"Palash","true"] // not valid

Bad behavior

Tuple has some bad behavior which it should not do but it is allowed anyways.

// bad behaviour
let UserTuple: UserTuple = [122,"Palash",true]
// since user tuple is just an array we can use array methods on it. 
// which should not happen because the purpose of tuple is to make 
// sure we have all the menthioned data types in order
UserTuple.push("tf")

if you reached till here and you liked what you read don't forget LIKE to SHARE it with other people. if you have any feedback or suggestions feel free to use the comment section. Hope you tag along till the end of this series and strengthen your coding armor by adding TypeScript to it. Happy coding!

About the author

  • "Hello, I'm Palash Dhavle, a passionate software developer with a Master's Degree in Computer Applications. I specialize in crafting Python and JavaScript code to build dynamic applications. Through my blog, I share insights from my coding journey and explore the world of software development. Join me in this adventure of creativity, innovation, app development, and problem-solving. Welcome!

No comments yet. Be the first to comment.

TypeScript Unlocked

Part 7 of 8

TypeScript is a JavaScript Wrapper which helps you to write better JavaScript code by introducing types. In this series we will learn how to Install and use TypeScript to write cleaner and better code

Up nextTypeScript Unlocked: Interfaces馃挕 This article is a part of TypeScript Unlocked Series on my blog. Every article in the series is short, crisp and filled with examples and code snippets. If you want to learn TypeScript from scratch for absolutely FREE!!! check it out. Interfaces...

More from this blog