【发布时间】:2019-03-04 00:22:36
【问题描述】:
duplicate issue 正在解决一个稍微不同的问题。这是特定于在 interface 中使用类型的。
我想在界面中使用string literal type。我确信我离这个问题的答案只有一点点改变。
下面是我的代码的简化示例,它显示了错误。
我需要更改什么才能让barFunction(baz) 没有以下 Typescript 错误?
// foo.ts
type Name = 'a' | 'b'
interface Bar {
x: Name
}
const n: Name = 'a'
const barFunction = (bar: Bar) => console.log(bar)
barFunction({ x: 'a' })
const baz = { x: 'a' }
barFunction(baz) // <-- error here
错误信息
Argument of type '{ x: string; }' is not assignable to parameter of type 'Bar'.
Types of property 'x' are incompatible.
Type 'string' is not assignable to type '"a"'.
错误信息截图:
【问题讨论】:
标签: typescript