TypeScript - Offirmo-team/wiki GitHub Wiki

Une surcouche de typage pour javascript. Voir aussi flow.

https://www.typescriptlang.org/

Introduction

News

Advocacy

Bonnes pratiques

Ecosystème

installation

https://github.com/typescript-eslint/typescript-eslint

yarn add -D typescript tslib
yarn add -D tslint ts-node eslint typescript-eslint-parser prettier
yarn add -D typescript-string-enums tsmonad
yarn add -D @types/node 

Utilisation

fichiers .ts ou .tsx

tool

langage

  • interfaces, types
  • basic types :
    • boolean
    • number
    • string
    • void (for function returns) can only be null or undefined let unusable: void = undefined;
    • Function
  • complex
    • enum enum Color {Red, Green, Blue}; let c: Color = Color.Green;
    • any let notSure: any = 4; let list: any[] = [1, true, "free"];
  • multiple
    • array string[] ou Array<string> matrix number[][]
    • tuple [string, number]
    • union padding: string | number
  • object
    • optional values {a: string, b?: number}
    • enforcing key type { [k: string]: string }

Exemple: JSON Object (https://github.com/Microsoft/TypeScript/issues/1897)

interface JSONObject {
    [k: string]: string | number | boolean | null | JSONArray | JSONObject
}
interface JSONArray extends Array<string | number | boolean | JSONObject | JSONArray> { }

Var definition

let x: string;
let y: [string, number];

type assertion (cast)

let strLength: number = (someValue as string).length;

cast forcé

return (call to api...) as any as Promise<Foo>

interface

function printLabel(labelledObj: { label: string }) {
    console.log(labelledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
interface LabelledValue {
    label: string;
}

function printLabel(labelledObj: LabelledValue) {
    console.log(labelledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
type DummyMember = {
  email?: string
  name: string
  admin?: boolean
}
function status(params: { auth: Auth, teamId: string }): Promise<StatusResult> {
...
}

types avancés

typings files

base: declare module 'murmurhash3js-revisited'

augmentation technique: http://ideasintosoftware.com/typescript-module-augmentation-vs-declaration/

Avancé

écosystème

typings

outils

testing types

tslint (deprecated)

https://palantir.github.io/tslint/

{
	"extends": "tslint:latest",
	"rules": {
		"comment-format": false,
		...
	}
}

Libs

npm install --save typescript-string-enums

import { Enum } from "typescript-string-enums"

export const Status = Enum("RUNNING", "STOPPED")
export type Status = Enum<typeof Status>

Problèmes rencontrés

TS2339: Property 'assign' does not exist on type 'ObjectConstructor'

should target="ES6" + having ES6 in lib[] (default)

TS2304: Cannot find name 'console'

bug. Either:

  • npm i -D @types/node
  • "lib": "DOM"
  • declare const console: any;

build is sub-packaged

Requiring directly a file from outside of the package. (maybe auto-added by IDE?)

TS2354: This syntax requires an imported helper but module 'tslib' cannot be found

add tslib...

TOSORT

⚠️ **GitHub.com Fallback** ⚠️