Traffic to TypeScript questions grew by an impressive 142% in the last year, enough that we left it off to avoid overwhelming the rest of the scale.
Stack Overflow Blog / September 6, ‘17
A bootstrapped (i.e. written in TS itself)
or
A statically typed superset of EcmaScript that compiles to plain JavaScript
* Just bring your own polyfills.
npm install -g typescript
or
yarn global add typescript
tsc --init
// tsconfig.json
{
"compilerOptions": {
"target": "es5", // ES3, ES2015, ES2016, ES2017, ESNEXT
"module": "commonjs", // es2015, amd, umd, system
"strict": true // strict type-checking options
}
}
"target": "es5",
"module": "commonjs",
// "lib": [],
// "allowJs": true,
// "checkJs": true,
// "jsx": "preserve", // react
// "declaration": true,
// "sourceMap": true,
// "outFile": "./",
// "outDir": "./",
// "rootDir": "./",
// "removeComments": true,
// "noEmit": true,
// "importHelpers": true,
// "downlevelIteration": true,
// "isolatedModules": true,
/* Strict Type-Checking Options */
"strict": true
// "noImplicitAny": true,
// "strictNullChecks": true,
// "noImplicitThis": true,
// "alwaysStrict": true,
/* Additional Checks */
// "noUnusedLocals": true,
// "noUnusedParameters": true,
// "noImplicitReturns": true,
// "noFallthroughCasesInSwitch": true,
/* Module Resolution Options */
// "moduleResolution": "node",
// "baseUrl": "./",
// "paths": {},
// "rootDirs": [],
// "typeRoots": [],
// "types": [],
// "allowSyntheticDefaultImports": true,
/* Source Map Options */
// "sourceRoot": "./",
// "mapRoot": "./",
// "inlineSourceMap": true,
// "inlineSources": true,
/* Experimental Options */
// "experimentalDecorators": true,
// "emitDecoratorMetadata": true
// dynamically typed: a and b can be any values
const aPlusB = (a, b) => a + b;
// statically typed: a and b can only be numbers
const add = (a: number, b: number) => a + b;
// concat inferred to return string
const concat = (a: string, b: number) => a + b;
number
string
boolean
object
undefined
symbol (ES6)
null
null
Function
Array
void
Tuple
any
never
class
interface
enum
Optional args
strictNullChecks
Structural (duck) typing
Generics
Control-flow analysis
Type definition files (.d.ts)
@types npm namespace
(npm install -d @types/jquery)
DefinitelyTyped
dts-gen
Automatic type acquisition
allowJS
checkJS
// @ts-check
Coding to interfaces
Testability and functional programming
Mixins
Simulated function overloading
Abstract, public, private, protected, readonly modifiers
@prashaantt