Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created July 31, 2018 18:16
Show Gist options
  • Save mattiamanzati/335e18bf58001b444a6831b000589e4f to your computer and use it in GitHub Desktop.
Save mattiamanzati/335e18bf58001b444a6831b000589e4f to your computer and use it in GitHub Desktop.
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
type Override<T, K extends keyof T, V> = Omit<T, K> & { [N in K]: V }
type AnyStateConfig = {
type: "initial" | "state" | "final"
states: {[K: string]: AnyStateConfig}
}
type EmptyStateConfig = {
type: "state"
states: {}
}
class State<C extends AnyStateConfig = EmptyStateConfig>{
constructor(
public readonly config: C
){
}
type<StateType extends AnyStateConfig["type"]>(type: StateType): State<Override<C, "type", StateType>>{
return new State({ ...(this.config as any), type})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment