英文:
How to create default type in an atom using typescript?
问题
我试图为一个对象创建一个原子。在原子下默认出现以下错误:类型'{}'无法分配给类型'Country | RecoilValue
属性'[WrappedValue_OPAQUE]'在类型'{}'中丢失,但在类型'WrappedValue
我应该如何修复这个问题?我是TypeScript的新手。
export interface Country {
capital: string;
continents: string;
}
import { Country } from "../types/country";
import { atom } from "recoil";
export const countryAtom = atom<Country>({
key: "countryAtom",
default: {},
});
(注意:这是您提供的文本的翻译,没有其他内容。)
英文:
I am trying to create an atom for an object. I get the following error under default in the atom: Type '{}' is not assignable to type 'Country | RecoilValue<Country> | Promise<Country> | Loadable<Country> | WrappedValue<Country>'.
Property '[WrappedValue_OPAQUE]' is missing in type '{}' but required in type 'WrappedValue<Country>'.ts(2322)
How can I fix this? I am new to typescript.
export interface Country {
capital: string;
continents: string;
}
import { Country } from "../types/country";
import { atom } from "recoil";
export const countryAtom = atom<Country>({
key: "countryAtom",
default: {},
});
答案1
得分: 1
你可以在创建原子时指定类型:
export interface Country {
captial: string;
continets: string;
}
export const countryAtom = atom<Country>({
key: "countryAtom",
default: {
captial: "example string 1",
continets: "example string 2",
}
});
英文:
You can specify the type during the atom creation:
export interface Country {
captial: string;
continets: string;
}
export const countryAtom = atom<Country>({
key: "countryAtom",
default: {
captial: "example string 1",
continets: "example string 2",
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论