[Typescript 5.4] NoInfer


// Before
declare function createFSM<TState extends string>(config: {
    // Without NoInfer, TS doesn't know which
    // TState is the source of truth
    initial: TState;
    states: TState[]
}): TState;
const example1 = createFSM({
    initial: "not-allowed",
    states: ["open", "closed"]
}) // "not-allowed" | "open" | "closed"

// after
declare function createFSM2<TState extends string>(config: {
    // With NoInfer, 'initial' is removed as a candidate
    initial: NoInfer<TState>;
    states: TState[]
}): TState;
const example2 = createFSM2({
    initial: "not-allowed", // Error: Type '"not-allowed"' is not assignable to type '"open" | "closed"'.
    states: ["open", "closed"]
})

 

posted @ 2024-01-22 03:03  Zhentiw  阅读(13)  评论(0编辑  收藏  举报