𝗧𝘆𝗽𝗲𝗦𝗰𝗿𝗶𝗽𝘁 𝗣𝗿𝗶𝗺𝗶𝘁𝗶𝘃𝗲 𝗗𝗮𝘁𝗮 𝗧𝘆𝗽𝗲𝘀

TypeScript lets you manage how you assign data to variables. You have two main ways to do this.

  1. Explicit assignment You tell TypeScript exactly what type a value is. Example: let str: string = "hello";

  2. Implicit assignment TypeScript guesses the type based on the value. This is called inferred typing. Example: let str = "hello";

Even with inferred typing, TypeScript protects your code. If you try to change a string to a number, TypeScript shows an error.

When should you use explicit assignment?

TypeScript uses duck typing for complex objects. If an object matches a specific structure, TypeScript uses that structure. It will not let you add new properties that do not exist in the original definition.

JavaScript primitive types in TypeScript:

Note on null and undefined: If you do not set a type, these values allow a variable to change to any type. If you explicitly set the type to null or undefined, the variable stays that type.

Source: https://dev.to/jsha/introduction-to-typescript-javascript-primitive-data-types-49ok