Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TypeScript Simple Types


TypeScript supports some simple types (primitives) you may know.

There are three main primitives in JavaScript and TypeScript.

  • boolean - true or false values
  • number - whole numbers and floating point values
  • string - text values like "TypeScript Rocks"

There are also 2 less common primitives used in later versions of Javascript and TypeScript.

  • bigint - whole numbers and floating point values, but allows larger negative and positive numbers than the number type.
  • symbol are used to create a globally unique identifier.

Type Assignment

When creating a variable, there are two main ways TypeScript assigns a type:

  • Explicit
  • Implicit

In both examples below firstName is of type string


Explicit Type

Explicit - writing out the type:

let firstName: string = "Dylan";
Try it Yourself »

Explicit type assignment are easier to read and more intentional.


Implicit Type

Implicit - TypeScript will "guess" the type, based on the assigned value:

let firstName = "Dylan";
Try it Yourself »

Note: Having TypeScript "guess" the type of a value is called infer.

Implicit assignment forces TypeScript to infer the value.

Implicit type assignment are shorter, faster to type, and often used when developing and testing.


w3schools CERTIFIED . 2022

Get Certified!

Complete the TypeScript modules, do the exercises, take the exam and become w3schools certified!!

$45 ENROLL

Error In Type Assignment

TypeScript will throw an error if data types do not match.

Example

let firstName: string = "Dylan"; // type string
firstName = 33; // attempts to re-assign the value to a different type
Try it Yourself »

Implicit type assignment would have made firstName less noticeable as a string, but both will throw an error:

Example

let firstName = "Dylan"; // inferred to type string
firstName = 33; // attempts to re-assign the value to a different type
Try it Yourself »

JavaScript will not throw an error for mismatched types.


Unable to Infer

TypeScript may not always properly infer what the type of a variable may be. In such cases, it will set the type to any which disables type checking.

Example

// Implicit any as JSON.parse doesn't know what type of data it returns so it can be "any" thing...
const json = JSON.parse("55");
// Most expect json to be an object, but it can be a string or a number like this example
console.log(typeof json);
Try it Yourself »

This behavior can be disabled by enabling noImplicitAny as an option in a TypeScript's project tsconfig.json. That is a JSON config file for customizing how some of TypeScript behaves.

Note: you may see primitive types capitalized like Boolean.

boolean !== Boolean
For this tutorial just know to use the lower-cased values, the upper-case ones are for very specific circumstances.


TypeScript Exercises

Test Yourself With Exercises

Exercise:

There are two main ways TypeScript assigns a type:



        

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.