D U C T I O N declare type StringOrNull<T> = T extends string ? string : null; I f t h e g i v e n t y p e T i s a s t r i n g . . . . . . t h a n re t u r n t h e t y p e s t r i n g . . . o t h e r w i s e re t u r n t h e t y p e n u l l .
N A L T Y P E S declare type StringOrNull<T> = T extends string ? string : T extends null ? null : never; . . . o t h e r w i s e i f T i s o f t y p e n u l l . . . r e t u r n t y p e n u l l . . . o t h e r w i s e re t u r n t y p e n ev e r
function error( message: string ): never { throw new Error(message); } function infiniteLoop(): never { while (true) { } } Th rowi n g Error s I n fi n i t e Loop
T I O N A L T Y P E S type Foo<T> = T extends { a: infer U } ? U : never; I f t h e g i v e n t y p e T h a s t h e p r o p e r t y a n o t i c e i s t t y p e a n d r e t u r n i t .
Y P E S CO N D I T I O N A L LY export class RootComponent { notes$: Observable<Notes[]>; constructor(private notes: NotesService) { this.notes$ = this.notes.loadNotes(); } }
R T Y P E S C O N D I T I O N A L L Y export class RootComponent implements OnInit { constructor(private store: Store<fromNotes.NotesState>) {} ngOnInit() { this.store.dispatch(new LoadNotes()); } } From where do I need to import the state? From where do I need to import the action? Why am I not receiving a return value?
T Y P E S C O N D I T I O N A L L Y export class LoadNotes implements Action { readonly type = NotesActionTypes.LoadNotes; } export class LoadNotesSuccess implements Action { readonly type = NotesActionTypes.LoadNotesSuccess; constructor(public payload: Note[]) {} }
F E R T Y P E S CO N D I T I O N A L LY switch (action.type) { case NotesActionTypes.LoadNotesSuccess: return setNotes(action.payload, slice); !!... function setNotes(notes: Note[], slice: NotesSlice): NotesSlice { return { !!...slice, entities: notes }; } Case Reducer
R T Y P E S C O N D I T I O N A L L Y export class RootComponent implements OnInit { notes$: Observable<Note[]>; constructor(private notesFacade: StoreFacade) { this.notes$ = this.notesFacade.all$; } ngOnInit() { this.notesFacade.loadNotes(); } } Creates Action + Dispatches Action Is created dynamically Offers consumable selectors
E R T Y P E S C O N D I T I O N A L L Y Self Dispatching Action Self Dispatching Action Self Dispatching Action Self Dispatching Action Reducer C o m b i n i n g a ct i o n t y p e & c a s e re d uc e r b u i l d i n g t h e red u cer fu n ct i o n .
Y P E S CO N D I T I O N A L LY function caseReducerA<T, P>(state: T, payload: P): T { !!... } function caseReducerB<T, Q>(state: T, payload: Q): T { !!... } function caseReducerC<T>(state: T): T { !!... } Or ... Or ...
payload: infer TPayload) !=> any ? (payload: TPayload) !=> void : never; i n f e r l e t s y o u ex t ra c t a t y p e b a s e d o n t h e u s a g e o f t h e re s p e c t i v e m e t h o d
N A L T Y P E S @Ducksify({ initialState: !!... }) export class NoteDucks { loadAll = effect('Load Notes‘); @Action('Loading Notes succeeded') set(State: State, notes: Note[]) { return { !!...state, entities: notes }; } Case Reducer Type @Component({ !!... }) export class RootComponent implements OnInit { constructor(@Inject(NoteDucks) private ducks: Duck<NoteDucks>) {} ngOnInit() { this.ducks.loadAll.dispatch(); } }
P E S CO N D I T I O N A L LY Exclude<T, U> Exclude from T those types that are assignable to U. Extract<T, U> Extract from T those types that are assignable to U. NonNullable<T> Exclude null and undefined from T. ReturnType<T> Obtain the return type of a function type. InstanceType<T> Obtain the instance type of a constructor function type. ConstructorParameters<T> Obtain the parameter types of a constructor function type. https://www.typescriptlang.org/docs/handbook/advanced-types.html
S Conditional types in TypeScript - Artsy Engineering TypeScript 2.8: Conditional Types — Marius Schulz Notes on TypeScript: Conditional Types - DEV Community *+ Conditional types in TypeScript – JavaScript everyday – Medium TypeScript: Create a condition-based subset types – DailyJS – Medium A Look at TypeScript’s Conditional Types TypeScript conditional types real-life example - codewithstyle.info https://www.reddit.com/r/typescript/comments/9n189u/how_are_you_using_conditional_types/