Basic TypeScript types

Before we go on, let's see what basic types are supported by TypeScript:

  • boolean: true or false.
  • number: Floating point values. Those can be expressed in hexadecimal, decimal, binary, and octal forms.
  • string: can be delimited by single quotes ('), double quotes ("), or back-ticks (`) to define template literals (also known as template strings).

Here are some examples of numbers:

  • let decimal: number = 42
  • let hexadecimal: number = 0x42
  • let binary: number = 0b101010
  • let octal: number = 0o52

Here are some string examples:

  • let hello: string = 'Hello'
  • let world: string = "World" // same as above
  • let cool: string = `${hello} ${world}!`

Template strings are very useful—they make it really easy to embed expressions. In the preceding example, we've just included another string, but in a template string expression, you could also invoke functions, perform calculations, and so on. Template strings usually help improve code readability. Note that string templates are actually a feature of ES2015 (that TypeScript supports by default).

You can also define multiline template literals!

TypeScript also supports other basic types that we will cover later on: arrays, tuples, enums, any, void, null, undefined, never, and object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.211.87