TypeScript
This document note some need-to-know things for TypeScript User.
Type Definition
Almin has built-in Type definition file(.d.ts
).
If you want to use almin with TypeScript, you just install Almin.
Type definition file(.d.ts
) will be loaded automatically.
Payload
You can use abstract Payload
class for creating own payload.
This Payload
class pattern is simple in TypeScript.
Example: StartLoadingUseCase
StartLoadingUseCase.ts
import { Payload, UseCase } from "almin";
export class StartLoadingPayload implements Payload {
type = "StartLoadingPayload";
}
export class StartLoadingUseCase extends UseCase {
execute() {
this.dispatch(new StartLoadingPayload());
}
}
Also, you just use instanceof
type-guard.
import { Payload, UseCase } from "almin";
import { StartLoadingPayload } from "./StartLoadingUseCase.ts";
export class ExampleStore extends Store {
receivePayload(payload: any) {
if(payload instanceof StartLoadingPayload){
// payload is StartLoadingPayload
}
}
// ...
}
Further reading
UseCase
You should use executor
method instead of execute
method.
Because, executor
is type-safe method, but execute
is not type-safe.
// OK
context.useCase(someUseCase).executor(useCase => useCase.execute(args))
// NG
context.useCase(someUseCase).execute(args);
This limitation come from TypeScript feature. For more details, see following link.
Helper
Examples
Almin + TypeScript real examples.