Skip to main content

Type Inference

In JavaScript, you're familiar with this concept here,right this is a variable declaration.

  • We're using let here, and let would allow us to reassign this variable, right?
  • TypeScript has inferred that this is a number based on us just saying that we're initializing this variable to 6.
  • This kind of inference takes place when initializing variables and members, setting parameter default values, and determining function return types.
let temperature = 6; //! inference
  • Re-assigning the variable to a string, TypeScript will throw an error.
  • Because when it variable is declare the inferred the type to the number then we can't reassign it to a string type or a change the type of variable that is not allowed.
let temperature = 6; //! inference
temperature = "warm"; //! type-checking

what's the difference between a const declaration and a let declaration?

  • It const is constant so you can't change the value.You can't reassign the variable common misconception that you can't change the value.
  • e.g So in this case it's actually both 79 is a number, and in JavaScript that's an immutable value type.We never change numbers, we can only create new numbers.So here we have non re assignability coming from const,but we also have immutability coming from 79.

Literal Types

  • Literal Types means we're using a value and creating a type that represents only that one value. Types like 79 are called a literal types — you can think of this as “only 79 is allowed”
const humidity = 79; //! literal type
  • String is also an immutable value type, const does not allow us to reassign.
const humidity = "foo"; //! literal type
  • For re-assign have re-assignability, but it's typed is still 79.I'm doing something here called casting where I'm saying,I have value here, this is the type I want you to regard this value as.
  • Humidity is allowed to be any number as long as it's the number 79It's only got one thing and it said it can only accept 79
let humidity = 79 as 79;
humidity = 80; // error because 80 is not 79
  • As opposed to if we made this an array like this,we can certainly change the value, we can push more things into the array, right? It's not like it's frozen in place, the variable always will point to that array,but we can change the array in place.
const arr = [10];
arr.push(20);
console.log(arr);

Type Casting

  1. If using let here, we're saying that we can reassign this variable letter, right? because the let keyword is typed inferred as a number.
  2. If we use this as const casting here,we're basically saying, I want the mutability of let,but treat it in terms of obtaining its type as if it's a const, right?
let temp2 = 19; //! temp2's type is { all numbers }
let humid2 = 79 as const; //! humidity's type is { 79 }
temp2 = 23; //! Is each member in { 23 } also in { all numbers }?
temp2 = humid2; //! Is each member in { 79 } also in { all numbers }?
humid2 = temp2; //! Is each member in { all numbers } also in { 79 }?
humid2 = 79; //! Is each member in { 79 } also in { 79 }
humid2 = 78; //! Is each member in { 78 } also in { 79 }
  • So sometimes we need to declare a variable before it's initialized.
  • We would call this an implicit any, any,you can think of as a type that represents the way variables work in JavaScript.
  • we would call this a top type.It can accept absolutely any value that is possible to create in JavaScript.

Conclusion

  • Type inference is a way to predict the type of a variable based on its value.
  • TypeScript uses type inference to provide static types to variables.
  • TypeScript can infer the type of a variable based on the value assigned to it.
  • TypeScript can infer the type of a variable based on the return type of a function.
let x = 10; // x is inferred as number
let y = "Hello"; // y is inferred as string
let z = true; // z is inferred as boolean

When creating a variable, there are two main ways TypeScript assigns a type:

  1. Explicit
  2. Implicit Explicit - Writing out the type