Javascript - Typescript
About
TypeScript is a typed superset of JavaScript that compile to plain JavaScript. It adds static typing.
Basic
See Typescript - tsc (official compiler)
When installing third party libraries (lodash, jQuery, …), it is important to remember to install the typing definition for that library.
JQuery
- Add Jquery as package dependency (because it's already available we declare it as peer dependency)
yarn add @types/jquery --dev
yarn add jquery --peer
- Add it to the compile path
{
"compilerOptions": {
....
"types": [
"jquery"
]
},
...
}
Scope
- Window: declare variable in the window scope. see Typescript - Global Declaration - How to declare a global variable
Feature
Type
type annotation. See Typescript - Type
Enum
export enum choice {
YES = 'Yes',
NO = 'No',
NA = 'Not applicable',
}
Interface
interface are type
Cast
Class
Classes in TypeScript are just a shorthand for the same prototype-based OO.
Support
Cannot find name 'name'.ts(2304)
The name is not imported you can define it also fully.
Example:
* @param {!Browser} browser
could be replaced with the fully qualified name
* @param {!import('puppeteer').Browser} browser