Node.js 删除typescript接口中的“不存在”属性

Node.js 删除typescript接口中的“不存在”属性,node.js,typescript,Node.js,Typescript,我在使用扩展运算符时遇到一些问题。 使用排列运算符时,创建的对象中存在意外属性。 我可以只筛选已定义的属性还是删除未定义的属性 type test = { id: string } const dump = { id: 'id', name: 'name' }; const testResult: test = { ...dump }; // wanted result { id: 'id' } console.log('test', testResult

我在使用扩展运算符时遇到一些问题。 使用排列运算符时,创建的对象中存在意外属性。 我可以只筛选已定义的属性还是删除未定义的属性

    type test = { id: string }
    const dump = { id: 'id', name: 'name' };
    const testResult: test = { ...dump };
    // wanted result { id: 'id' }
    console.log('test', testResult); // test { id: 'id', name: 'name' }

这是你应该做的:

type test = { id: string }
  const dump = { id: 'id', name: 'name' };
  const testResult: test = {id:dump.id}

我认为问题不仅仅在于打字脚本。Typescript在检查类型方面非常自由。在某种意义上

interface IFoo {}
interface IBar {
name: string
}

// No error will be thrown
const foo: IFoo = { name: 'sam' };
const bar: IBar = { name: 'sam' };

// following line will throw error. As IBar expects the property name.
const justFoo: IBar = { greet: 'hello' };
此处
foo
可用作
IFoo
IBar
类型之一的变量。两者都可以。但在
justFoo
的情况下不是这样,它只能是IFoo