A typed superset of JavaScript Developed by Microsoft it Compiles to JavaScript for either Browsers or Node there are Three parts: Language, Language Service and Compiler.
When we read typescript, it consists of type and script so what is type.
Type as we know there are tow type of type variable
Fist type is Primitive Types
Which the value of type it is Six primitive types in JS:
Null -undefined -boolean-number-string-symbol(it add in 2015)
The second type is object or reference it maybe object or function or variable pointer to oner object.
As we know Type Conversion
As with many dynamically-typed languages, things are converted “as needed”
30 + 7; //37
‘37’ + 7; //”37”
‘37’ - 7;// 30
(+ '37’) ;//37
(+ false) ;//0
Some it generates some error
Sometimes JavaScript does unintuitive things to convert primitive types
const login = (username: string, password: string): User => {
!/ do something !*
}
Some time we need to convert it from type to another by keyword “as”
document.querySelector('input#name_field') as HTMLInputElement;
"green",
() => {}
]);
const objectCar: { name: String, number: Number}={
name:"dsdsds",
number:10,
}
If any error in the shape the compile it will unhappy it put red line in the location when it not happy
name:"dsdsds",
number:"dsddds",
}
number must show message Type ‘string’ is not assignable to type ‘Number’
#javascript #typescript #node-js #js #reactjs