
Array Type Inference
TypeScript can infer variable types from initial values and return statements. Even still, we may not know exactly what type inference to expect when dealing with arrays. For example:
let examAnswer = [true, false, false]
What is the type of examAnswers? It seems it could equally well be boolean[] or [boolean, boolean, boolean]. In reality, it is always the first of these, since this is the less restrictive type. This enables us to expand the array:
examAnswers[3] = true // No type error.
Since tuples have fixed lengths, we wouldn’t be able add additional boolean elements to a tuple:
let tupleOfExamAnswers: [boolean, boolean, boolean] = [true, false, false]
tupleOfExamAnswers[3] = true // Type error! The tuple only has 3 elements.
Related articles
10 Essential Steps for Migrating Your JavaScript Project to TypeScript in 2025
Discover the practical roadmap to seamlessly transition your JavaScript projects to TypeScript, enhancing type safety and developer productivity in 2025.
Crafting Dynamic Data-Driven User Interfaces with React and TypeScript
Learn how to effortlessly craft dynamic, data-driven user interfaces using React and TypeScript, enhancing your app's responsiveness and user experience.
Mastering Next-Gen API Security: JWTs with Node.js & TypeScript in 2025
Learn step-by-step how to enhance your API’s security using JWTs with Node.js and TypeScript, preparing your applications for the security demands of 2025.