Static types
Structural type system only cares about the shape of the object, not to verify instance of particular object
Variables
Type inference
Just hover below line in .ts file
- TS automatically does type inference where ever possible (especially when direct value assigned).So no need to explicitly mention in such cases.
- It's more useful for class variables and method params & return types ..etc
Constants
- so for constants, it's not data type - it's exact value as type (as they are readable)
Not initialized
Static data types
- Any //good to work with existing JavaScript
- Boolean
- Number
- String
- enum //use const
- Array -> number[], string[], particularType[], any[]
- ParticularObject/ interface
- Void //normally return type of functions that do not return a value
- Null, undefined
- Never
Note: Use currently only bold items , also for readonly in classes better use const
Arrays
Default array type 'never', so array type must be changed at least to any to have values in it
marks:number[] = [];marks:string[] = [];marks = [1]; //just hover on variable to see type inference
Tuples (TS_SPECIFIC*)
Array with fixed length
//check docs
Particular object
When we define a object type, then object initialization needs all properties mandatory.
Note : with in an object ‘;’ semi colon is used – so you can understand that it is not value but type (prettifier can add for you )
optional operator (?)
Particular object with key access
(Index signature)
Index signatures describe how a type will respond to property access
Interface
To re-use this type, create an interface
Note: ctrl + click a type to navigate to definition
UNION & INTERSECTION
(TS_SPECIFIC*)
Consider
An intersection type combines multiple types into one
An Union type is either of types
Null and Undefined
Example : In cases where you want to pass in either a string or null or undefined (for functions)
Never
type of values that never occur or function that never returns