Typescript:将必需属性转换为可选属性

Typescript:将必需属性转换为可选属性,typescript,Typescript,如何将非可选属性转换为可选属性? 代码如下: interface Foo { bar: any // no '?', hence this prop is required } type KeysOfFoo = { [K in keyof Foo]: any } const keysOfFoo: KeysOfFoo = {} // No tsc error wanted here, got: "type '{}' is not assignable to type KeysOfFoo

如何将非可选属性转换为可选属性? 代码如下:

interface Foo {
  bar: any // no '?', hence this prop is required
}

type KeysOfFoo = {
  [K in keyof Foo]: any
}

const keysOfFoo: KeysOfFoo = {} // No tsc error wanted here, got: "type '{}' is not assignable to type KeysOfFoo"

这里只有一个变化,尽管它不适用于问题中的问题。这里的解决方案还保留了键的类型。。 请在此处阅读更多信息:

接口Foo{
栏:任何//否'?',因此需要此道具
}
类型KeysOfFoo={
[K in keyof Foo]?:Foo[K]//这样它就保留了类型
}

看一下
部分
类型:谢谢,你给了我正确的方向。我刚刚在“key”部分之后添加了问号:
[K in keyof Foo]?:any
如果链接文档中没有立即清除,则
Partial
是TypeScript的一部分。另外还有,谢谢!我得到了它。在我的情况下,我不能使用
Partial
,因为我的值不是
Foo[K]
,而是完全不同的值