io-ts:仅引用类型,但在这里被用作值



我是一个刚接触TypeScript的新手,遇到了以下问题:

import * as t from "io-ts";
interface Test {
s: string
}
export const testTypeV = t.type({
test: Test //Error: 'Test' only refers to a type, but is being used as a value here
})

代码有什么问题?如何解决这个问题?

在您的代码中,您没有声明testTypeV的类型,而是进行变量赋值-编译器抱怨您试图将密钥test的值分配给类型。相反,您需要一个类型为Test的值,例如:

export const testTypeV = t.type({ test: { s: 'any-string' } });

testTypeV的类型是t.type返回的类型。

最新更新