type inference
The compiler working out a type from the code itself, so you do not have to write it out by hand.
In more detail
The type is still fixed once chosen; inference only saves typing, so code stays short while remaining statically typed. For example, a variable set to a string literal is given a string type without you writing it. An explicit type can still document an unusual case.
Examples
type UserDTO = z.infer<typeof UserSchema>ratio := 0.75 // float64Go: host := "db-1" infers string; ratio := 0.75 infers float64Java: String answer = first("ready", "later"); var names = List.of("Ada", "Lin");String answer = first("ready", "later");var names = List.of("Ada", "Lin");Related terms
- Zod — A TypeScript library for declaring validation schemas that check data at run time and also produce matching static types.
- typeof — A keyword that asks for the type of a value, both in JavaScript at run time and in TypeScript types.
- DTO — A plain object whose only job is to carry data across a boundary, such as the body of an API request or response.
- tRPC — A TypeScript framework that lets a frontend call backend procedures with full type safety by sharing the server’s types, with no code generation.
- short variable declaration — The := form that declares a new variable and sets its value in one step, letting Go infer the type.
- static typing — A language rule that every variable has one fixed type, known when the program is compiled, so type mistakes are caught before it runs.
- int — The common integer type in many languages, holding whole numbers within a fixed range set by its size in bits.
- floating-point number — A number with a fractional part stored in binary, which can only approximate most decimal values.
- diamond operator — The empty angle brackets in new ArrayList<>(), which ask the compiler to infer the constructor's type arguments.
- generics — Types that take other types as parameters, so one definition works for many kinds of value.
- type annotation — A type written explicitly after a name with a colon, such as name: string, to tell the compiler what a value must be.
- contextual typing — Type inference that flows from where a value is used, such as giving a callback's parameters the types the calling function expects.
- literal type — A type that allows exactly one value, such as the string "GET", the number 404 or true.
- type widening — The compiler giving a value a broader type than its exact value, such as string instead of "production", because it might change later.
Taught in
- End-to-End Type Safety: Shared Schemas & DTOs — Production Full-Stack Engineering
- Type-Safe RPC Architectures: tRPC & Procedures — Production Full-Stack Engineering
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Generics Fundamentals — Modern Java: Virtual Threads & High-Throughput Services
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- Fluent Assertions With AssertJ — Modern Java: Virtual Threads & High-Throughput Services
- Scope, var And Immutable Objects — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- tsconfig.json: target, lib, strict And NodeNext — Production TypeScript
- Type Annotations, Type Inference And Contextual Typing — Production TypeScript
- Verify Your Environment: Running tsc And Bun TypeScript — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Custom Type Predicates: Functions Returning is Type — Production TypeScript
- Why Generics: Functions, Identities And Type Variables — Production TypeScript
- Default Generic Arguments And Multi-Type Parameters — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- The satisfies Operator: Safe Validation Without Widening — Production TypeScript
References
- TypeScript: Type inference
- Zod: Inferring types
- The Go spec: short variable declarations
- Java Tutorials: Type Inference
- Java Language Specification, chapter 15: Expressions
- TypeScript Handbook: Everyday Types