【问题标题】:Recoil Atom and Typescript, define type and pass atom as parameterRecoil Atom 和 Typescript,定义类型并将原子作为参数传递
【发布时间】:2022-12-22 07:58:52
【问题描述】:

我开始使用反冲和打字稿。

此时,我使用默认属性来定义我的原子的每个属性类型:

 const WipStateAtom = atom({
    key: 'wipAtom',
    default: {
       data: null as IData | null,
       ex: null as IEx | null,
    }
 });

但是是否可以使用接口/或类型来定义原子内容,例如:

export interface IWipAtom {
   data: IGameData | null,
   ex: IEx | null,
}

我想在函数中传递一个原子,但我不想将其键入 ANY :

const [wip, setWip] = useRecoilState(WipStateAtom);

const myFunction = (thewip: any) => { ... }

我更喜欢严格定义它的类型:

const myFunction = (thewip: IWipAtom) => { ... }

有什么方法可以输入原子吗?

【问题讨论】:

    标签: reactjs typescript recoiljs


    【解决方案1】:

    您可以在原子创建期间指定类型:

    const WipStateAtom = atom<IWipAtom>({
      key: 'wipAtom',
      default: {
        data: null as IData | null,
        ex: null as IEx | null,
      }
    });
    

    【讨论】:

    • 对我感到羞耻......仿制药......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多