TypeScript Reference ยท Type Operators
TypeScript definite assignment assertions
Reference for definite assignment assertions in TypeScript: the name!: Type syntax for class fields and variables that are set later, and when to avoid it.
With strictPropertyInitialization (part of --strict), every class property whose type does not include undefined must be assigned in the declaration or the constructor. The compiler also complains if a let variable is read before it is definitely assigned.
A definite assignment assertion is the ! placed after the name, before the colon: name!: string. It says "trust me, this is set before anyone reads it" and switches the check off for that one declaration. It is typically used for fields filled in by a framework, a lifecycle method, or an init() call.
class Widget { element!: HTMLElement; // assigned in init() }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
class A { prop!: Type; static shared!: Type; } let variable!: Type;
| Form | Meaning |
|---|---|
prop!: Type |
The property is considered initialized; no error if the constructor does not assign it. |
let x!: Type |
The variable is considered assigned before use. |
Errors and edge cases:
- Without it, the compiler reports "Property 'nickname' has no initializer and is not definitely assigned in the constructor" (TS2564).
- It requires a type annotation.
prop! = 1is invalid. - It is not allowed on a property that already has an initializer (an error: a definite assignment assertion cannot be used with an initializer).
- It cannot be used in ambient contexts or with an optional property (
prop?!is invalid).
Usage notes
- No runtime effect. The field is simply
undefineduntil you set it; withuseDefineForClassFields(default for ES2022 and later targets) the field is created asundefinedat construction. - Alternatives: make the type
Type | undefinedand narrow before use; give the field a default value; assign in the constructor; use an optional fieldprop?: Type. - Dependency injection and ORMs (decorators on fields) are the usual reasons for
!:. - Do not confuse with the non-null assertion
x!used in expressions. - Order of initialization: if a base constructor calls a method that reads the field, it still sees
undefined. - Linters treat it as a smell; document why the field is guaranteed.