简体   繁体   English

带传播算子的打字稿题词

[英]Typescript insection with spread operator

I'm trying to do pretty trivial: Pass a typed object into a function, augment it with known keys, and return a type that intersects the two. 我正在尝试做一些琐碎的事情:将一个带类型的对象传递给一个函数,用已知的键对其进行扩充,然后返回一个与两者相交的类型。

The following code seems to get me most of the way 以下代码似乎使我受益匪浅

interface Success {
  success: boolean
}

interface Named {
  name: string
}

type Resolver<T> = Success & T

function test<T>(input: T): Resolver<T> {
  return {
    success: true,
    ...input
  }
}

const person: Named = { name: 'Bob' };
const res = test<Named>(person);
// `res.success` and `res.name` are inferred

However, I get the following error on the ...input spread operator: 但是,我在...input传播运算符上收到以下错误:

传播操作员错误

How do I coerce T to be an object? 如何强迫T成为对象? I've tried creating an indexed interface and extending that, but the same error persists. 我尝试创建一个索引接口并进行扩展,但是仍然存在相同的错误。

This is a TypeScript bug. 这是一个TypeScript错误。 It will be fixed by PR https://github.com/Microsoft/TypeScript/pull/13288 它将由PR https://github.com/Microsoft/TypeScript/pull/13288修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM