ReasonJun

Typescript : Inference 본문

Frontend/Typescript

Typescript : Inference

ReasonJun 2023. 6. 10. 00:49
728x90

In TypeScript, type inference refers to the process by which the TypeScript compiler automatically determines the type of a value or expression based on its usage and context. TypeScript's type inference system analyzes the code and infers types for variables, function return values, and other expressions, reducing the need for explicit type annotations in many cases.

Here are some key points about type inference in TypeScript:

  • Variable Initialization: When you declare a variable and initialize it with a value, TypeScript infers the type of the variable based on the type of the assigned value. For example:
let message = 'Hello'; // TypeScript infers `message` as type `string`
let count = 42; // TypeScript infers `count` as type `number`
  • Function Return Types: TypeScript can infer the return type of a function based on the types of its return statements. If a function has a return statement with a specific type, TypeScript will use that as the inferred return type. For example:
function add(a: number, b: number) {
  return a + b; // TypeScript infers the return type as `number`
}
  • Contextual Type Inference: TypeScript infers types based on the context in which values or expressions are used. For example, when assigning a value to a variable or passing an argument to a function, TypeScript uses the expected type in that context to infer the type of the value or expression. This allows for more concise code with fewer explicit type annotations. For example:
let numbers = [1, 2, 3]; // TypeScript infers `numbers` as type `number[]`
numbers.push(4); // `push` expects `number`, so TypeScript infers `4` as type `number`
  • Type Inference with Union and Intersection Types: TypeScript can infer types when working with union types and intersection types. If a value can have multiple types (union type), TypeScript will infer the common type shared by all the possibilities. If a value combines multiple types (intersection type), TypeScript will infer the combined type. For example:
let value: string | number = 'Hello'; // TypeScript infers `value` as type `string | number`
value = 42; // TypeScript infers `42` as type `number`
  • Type Inference with Type Guards: TypeScript uses type guards, such as typeof, instanceof, and custom type predicates, to narrow down types and provide more specific type information. Type guards help TypeScript infer more precise types based on conditional checks or runtime checks. This enables better type checking and type inference in complex scenarios.

Type inference in TypeScript enhances developer productivity by reducing the need for explicit type annotations while still providing strong static type checking. It allows for cleaner and more concise code without sacrificing type safety. However, explicit type annotations can still be used when needed to provide clarity or when the type inference is not sufficient.

// Inference
// 'Inference' - Deriving another judgment based on one judgment.

// 1) initialized variable
// 2) Parameters with default values 
// 3) Functions with Conversion Values

// initialized variable
let num1 = 12
// num1 = 'Hello type!' >>> error

// Parameter 'b' with default value + function 'add' with definite return value
function addd(a: number, b = 2) {
    return a + b
}

 

728x90
Comments