TypeScript Type Annotations
TypeScript uses type annotation to explicitly type things such as variables, functions, and classes.
For the most part, TypeScript does a good job at inferring types from the codebase at build-time. However, some constructs in TypeScript, such as variables without an initial value or members of interfaces, don’t have an initially inferable type. This is where type annotations can be really useful.
Syntax
Type annotations consist of a : colon followed by the name of a type.
They exist after the name of the construct they’re declaring the type for.
// Class and interface membersclass myClass {memberOne: string;memberTwo: string;}interface myInterface {memberOne: string;memberTwo: number;memberThree: boolean;}// Function Parametersfunction myFunc(memberOne: string, memberTwo: string) {...}// Function Returnsfunction myFunc(memberOne, memberTwo): string {return memberOne + memberTwo;}// Variableslet myVar: string = "Hello, World!";
Type Annotations and Dates
In the following code snippet, the date variable doesn’t have an initial value, but with a Date type annotation. Later on, when date is assigned a value, it must receive a value of type Date. The first assignment of a new Date is the right type. However, the second assignment to a number, 10.31, is not:
let date: Date;date = new Date('2021-10-31'); // Okdate = 10.31;// Error: type 'number' is not assignable to type 'Date'.
After TypeScript is Compiled
TypeScript type annotations don’t get compiled down to the output JavaScript, as they are a TypeScript syntax and not JavaScript. Removing comments from the above code would make it look roughly like this if run through the TypeScript compiler (tsc):
let date;date = new Date('2021-10-31');date = 10.31;
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn TypeScript on Codecademy
- A full-stack engineer can get a project done from start to finish, back-end to front-end.
- Includes 51 Courses
- With Professional Certification
- Beginner Friendly.150 hours
- Learn TypeScript, a superset of JavaScript that adds types to make the language scale!
- Intermediate.10 hours