[英]Typescript variable with type and undefined
interface Data {
name: string,
address: string
}
interface Detail {
detail: Data | undefined
}
export const Profile: Detail {
profile: undefined
}
Here I have initially defined profile
as undefined
.在这里,我最初将
profile
定义为undefined
。 It works fine.它工作正常。 I can update prifle with
Data
type.我可以使用
Data
类型更新 prifle。
What I want to achieve here is at some point I should be able to update profile
with undefined
.我想在这里实现的是在某些时候我应该能够使用
undefined
更新profile
。
I can initialize with undefined
but I can not update it with undefined.我可以使用
undefined
进行初始化,但无法使用未定义进行更新。
How can I make it to take either Data
type or undefined
我怎样才能让它采用
Data
类型或undefined
You are missing an = sign, the last three lines should be您缺少一个 = 符号,最后三行应该是
export const Profile: Detail = {
profile: undefined
}
The Botly Noob is right, also you have to modify the param name, otherwise it won't be able to be assigned to the type Detail, Object literal may only specify known properties, and 'profile' does not exist in type 'Detail' Botly Noob 是对的,你也必须修改参数名称,否则将无法分配给 Detail 类型,Object 字面量只能指定已知属性,并且 'profile' 不存在于 'Detail' 类型
Are you looking for something like:您是否正在寻找类似的东西:
interface Data {
name: string;
address: string;
}
export const Profile = {
profile: Data | undefined;
}
I couldn't really understand the usecase of Detail
interface.我无法真正理解
Detail
接口的用例。
Try null instead of undefined.尝试 null 而不是未定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.