Type inference
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
You do not have to write a type on every variable. Follow a five-line file with no types written at all as tsc, the TypeScript checker, works out each one from its starting value: count and total are numbers, label is a string, and the const unit gets the exact type "kg". Then watch the type it inferred catch count = "three" before the program runs.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What is type inference?
With let count = 3, tsc sees the starting value 3 is a number and gives count the type number, as if you had written let count: number = 3.
02What type does TypeScript infer for let total = count * 2, when count is a number?
tsc looks up count (a number); a number times 2 is a number. Inference works through whole expressions, using types it already knows.
03What type does TypeScript infer for const unit = "kg"?
A const can never get a new value, so tsc gives it the narrowest type: exactly "kg". With let unit = "kg" it would infer string, because a let can change later.
04In this file nobody wrote a type. What happens with count = "three" on line 5?
count was inferred as number on line 1, and every later use is checked against it. tsc reports error TS2322 on line 5, before the program runs.
05Where do you usually still write a type yourself?
A parameter has no starting value, so tsc cannot infer it from the call. Without an annotation it becomes any (an error with noImplicitAny). Variables with a starting value are inferred.
المسرد
- Type
- What kind of value something is, such as number (3, 2.5) or string (text in quotes, like "apples").
- Type annotation
- A type you write yourself next to a name, like : number in let count: number = 3.
- Type inference
- TypeScript working out a type from the code, usually from a variable's starting value, so you do not have to write it.
- tsc
- The TypeScript compiler. It infers and checks types, reports errors, and writes plain JavaScript.
- Literal type
- A type that is exactly one value, such as "kg". A const with a simple value gets one.
- let and const
- Two ways to make a variable. A let can be given a new value later; a const cannot.
- any
- A type that turns checking off for a value. A parameter with no annotation becomes any.
- Hover
- Resting the mouse on a name in an editor such as VS Code; it shows the type tsc inferred.
الملاحظات والمصادر
- The error text, the four inferred types and the hover text were checked with TypeScript 5.9 (tsc and its checker) on this exact file.
- A function parameter with no annotation is not inferred from how you call the function: it becomes any, which the noImplicitAny setting (on in strict mode) reports as an error. The exception is contextual typing, such as a callback passed to forEach, where TypeScript knows the type from where the function goes.
- tsc also infers what a function returns from its return statements, so function half(n: number) { return n / 2; } returns number without you writing it.
- The fixed file has no errors. It had no type annotations to remove, so the JavaScript tsc writes is the same code; with no settings, tsc 5.9 only rewrites let and const as var, for older browsers.
- TypeScript Handbook: Type Inference
- TypeScript Handbook: Everyday Types (type annotations on variables, literal types)
- TypeScript Handbook: Everyday Types, Contextual Typing
- TypeScript Handbook: The Basics (static type checking, tsc)
- TSConfig Reference: noImplicitAny
- MDN: let
- MDN: const
- Visual Studio Code: TypeScript in Visual Studio Code
اختبار يتاح بعد الخطوة الأخيرة.