简体   繁体   English

Typescript 不想通过排除传播 object 类型

[英]Typescript does not want to spread type that's object by exclusion

I'm having issues spreading an externally imported type that's an object by way of Exclude .我在通过Exclude传播外部导入类型object时遇到问题。 To demonstrate:展示:

const f = (): object => ({});
const v = {...f()}; // valid -> OK
const f = (): object | string => ({});
const v = {...f()}; // invalid -> OK
const f = (): Exclude<object | string, "string"> => ({});
const v = {...f()}; // invalid -> PROBLEM

In the last snippet I would expect the Exclude to make the type valid.在最后一个片段中,我希望Exclude使类型有效。 Is this me thinking about it wrongly, or is there another way of fixing this?这是我想错了,还是有另一种解决方法? (the type exemplified by object is an external import I can't change). (以object为例的类型是我无法更改的外部导入)。

You made a typo in the exclude : You want to exclude the type string not the type "string" (which is a string that can only have the value "string" )您在exclude中打错了:您要排除类型string而不是类型 " string "string" (这是一个只能具有值"string"的字符串)

const f = (): Exclude<object | string, string> => ({});
const v = { ...f2() }; // ok

Playground 操场

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

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