Types

Similar to how you can create a container for a value with let or const, TypeScript allows you to create a container for a data type with the type keyword.

The name greeting refers to an actual unit of information whose data type is string. The name Salutation refers to the data type string, and only exists within the TypeScript compiler. When TypeScript compiles the program, it will only emit one line of code.

TypeScriptJavaScript
compiling...

If you attempt to use a type as a value, the TypeScript compiler will produce an error.

You can even use the same name for a variable and for a type, although this is strongly recommended against for readability reasons.

To summarize this important distinction between data and types:

  • Data is the actual information that exists - the 0's and 1's that your program manipulates when it runs.
  • Types are metadata used by TypeScript to describe the shape or contract of data. They exist for the purpose of catching errors before your program runs, by enforcing certain rules during the compilation process.

Was this page helpful?