Basics
Challenges
Classes
Debugging
Events
External Files
Flow Control
Forms
Functions
Html Elements
Installation
Interfaces
Keywords
Modules
Namespaces
Operators
Reference Files
String
Types
By default in Javascript, the any
type can be ... any .. type. In addition, by default,
null and undefined are considered to be any
types since they cannot be determined by the
javascript compiler.
// this produces NO errors.
let basicString: string;
let undefinedType: undefined;
let anyType: any;
let nullType: null;
basicString = undefinedType;
basicString = anyType;
basicString = nullType;
This has lead to countless javascript bugs. You can explicitly prevent this in TypeScript by explicitly
setting the compiler option strictNullChecks
to true. Then and only then will your variables
be protected from being null/undefined/any.
Should you expect the variable you create to have a null value, you can use the Union Types
to set it to be either your decided primitive type or null
.