Skip to content

Latest commit

History

History
481 lines (410 loc) 路 19.2 KB

utils-typescript.md

File metadata and controls

481 lines (410 loc) 路 19.2 KB

TYPESCRIPT UTILS DEV

TYPESCRIPT GUIDES

START

npm install -g typescript 
tsc --init
tsc --watch 
npm init

TOOLS

USEFUL

TODO

STARTER

DEVTOOLS

UTILS

LIB: EXTENSION

LIB: SERVER

LIB: ALL

LIB: ASSEMBLYSCRIPT / WASM

LIB: DATABASE / SQL

LIB: PARSER

LIB: ASYNC / STREAM / REACTIVE

LIB: UI

LIB: CALC

LIB: FILE

LIB: SEARCH

LIB: COMPONENTS

LIB: AI

LIB: FUNCTIONNAL PROGRAMMING

LIB: GENERATE DATA OBJECT

LIB: DATA OBJECT

LIB: HTTP

LIB: TEST

LIB: ERROR

NEWS

TIPS

ES2015/ES6 + TYPESCRIPT COURSES

CLASS OR INTERFACE

classe si besoin de creer des function sur le pojo ou bien interface suffit si classe alors utiliser mecanisme fromJSON (ng-book2 screencast final-app voir code source)

CLASSES INHERIT

GENERIC SERVICE

DECORATORS

SYNTAX

typescript types - https://www.typescriptlang.org/docs/handbook/basic-types.html typescript tuto - http://www.typescriptlang.org/docs/tutorial.html interface vs classe typescript interface - https://www.typescriptlang.org/docs/handbook/interfaces.html typescript classe - http://www.typescriptlang.org/docs/handbook/classes.html typescript .d.t - https://github.com/DefinitelyTyped/DefinitelyTyped /// typescript arrays - http://www.codecompiled.com/arrays-in-typescript/

typescript interface 	interface HasScore { 聽 score: number; } 聽   function addPointsToScore(player: HasScore, points: number): void { 聽     player.score += points; 聽   } 
typescript interface	interface CanRun { 聽 run(meters: number): void; } 聽   function startRunning(pony: CanRun): void { 聽     pony.run(10); 聽   } 
typescript classe		class NamedPony { 聽 constructor(public name: string, private speed: number) { 聽 } 

typescript optionnal 	public votes?: number; 		+ this.votes = votes || 0; 
typescript types 		const poneyNumber: number = 0; 	const poneyName: string = 'Rainbow Dash';  
   					const pony: Pony = new Pony(); const ponies: Array<Pony> = [new Pony()]; 
typescript type retour  function startRace(race: Race): Race {  
typescript enum			enum RaceStatus {Ready, Started, Done} 聽   const race = new Race(); 聽   race.status = RaceStatus.Ready; 

es6 features - http://es6-features.org/#Constants es6 collections const users = new Map(); OU const users = new Set(); es6 iterer for (let user of users) { 聽 console.log(user.name); } es6 template const fullname = Miss ${firstname} ${lastname};

CHEATSHEETS

Object literal map:

exampleObject: { [key: string]: string };

Object literal from enum:

exampleObject: {
	[key in IdOptionEnum]: string;
} = {
	[IdOptionEnum.UID]: 'uid',
	[IdOptionEnum.UUID]: 'uuid',
	[IdOptionEnum.YUID]: 'yuid'
};

Define value from enum

enum Size {
	Small = 100
	Medium = 200
	Big = 300
}
exampleObject: keyof typeof ImageSize = 'Medium';

Generic values type from key of type

type Values<T> = T[keyof T];

Object vs primitive type

// primitive type like bool, number, string, symbol.
const o: object; // You can't assign to primitive type neither null or undefined
const q: { [key: string]: any }; // You can't assign to primitive type neither null or undefined
const p: {}; // or Object, you can assign to primitive type but not null or undefined

Typescript union vs generic,

  • union types do not exist at compile-time, use union when the types are known at compile-time,
  • programs that need access to the enumerated values should probably use an object instead.
  • use kind field to make a discriminated union and thus make difference easily recognisable
  • we can differentiate between types in a union with a type guard using the field kind or simply with the value.
  • keywords: typescript conditional types / typescript identity / typescript Narrowing / typescript mixins / type guard

EXTENDS

// https://stackoverflow.com/questions/64034690/typescript-extend-interface-dynamically-by-adding-new-fields-with-certain-namin
interface ProjectCostData {
  purchasePrice: number;
  propertyValue: number;
  recentlyDamaged: boolean;
}

// Create a new setter property for each key of an interface
type WithSetters<T extends { [k: string]: any }> = T & {
    [K in keyof T & string as `set_${K}`]: (newValue: T[K]) => void

// Testing
declare const obj: WithSetters<ProjectCostData>
obj.purchasePrice // number
obj.set_purchasePrice(123) // (newValue: number) => void
}

IMPORTS PATH

Imports within the same entry-point聽have to be聽relative, otherwise they would be considered to import from an external package/entry-point. On the other hand, imports into other entry-points聽must never be聽relative. If these rules are not followed, you run into all sorts of issues. Conceptually, this difference is essential to be able to look at an import and know where it is resolved from (i.e. whether it crosses an entry-point boundary or not) TypeScript path mappings shouldn't be used to make imports cleaner, shorter, sexier or whatever adjective you want to use. But rather used to resolve dependencies at design time that would otherwise not be automatically resolved, which is what it was designed for.

COURSES

UTILITY TYPES

Utility types used to create a new type based on an original type, they are also called transformation types. -Partial returns a new type with all the properties of the T interface optionals. -Required returns a new type with all the properties of the T interface required. -Readonly returns a new type with all the properties of the T interface readonly. -Pick returns a new type with only the picked properties of the T interface. -Pick returns a new type with only the few omitted properties of the T interface. -Exclude<T,U> returns a new type with the U type excluded from the T interface. -Extract<T,U> returns a new type with types assignable to U extracted from the T interface. -NonNullable returns a new type with only the non nullable properties of the T interface. -ReturnType returns the return type of a function passed with the typeof F. -InstanceType returns the return type of a class passed with the typeof C. -Record<K,T> is used to describe a javascript map, K is the type of the key and T the type of the value.

NEXT