如何验证RunTypes中具有未知属性的对象(打字稿小写对象)

时间:2020-09-14 14:17:39

标签: typescript runtypes

我只想验证对象并编写:

export const ExternalLinks = Record({})

合适吗?我问有关存储库的问题。

1 个答案:

答案 0 :(得分:1)

这样做是不对的。如果您以这种方式定义运行类型,它将接受任何内容。例如:

const MyRecordRuntype = Record({}); 

type MyRecord = Static<typeof MyRecordRuntype>; // {}

const greeting: MyRecord = 'Oh hai, Mark!';

const inquiry = "What's new with you?";

MyRecordRuntype.guard(inquiry); // true

我认为您想定义如下类型:

type MyRecord = { [key: string]: unknown };

我也不知道如何用 runtypes 做到这一点。另一种方法是使用 io-ts 代替。 There 你会找到 UnknownRecord