How to validate an object with unknown properties in RunTypes (typescript lowercase object)

Viewed 636

I just want to validate object and have written:

export const ExternalLinks = Record({})

Is it proper? I ask regarding the repository.

1 Answers

It is not correct to do that. If you define a runtype in this way it will accept anything. For example:

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

I think you want to define a type like the following:

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

I don't know how to do this with runtypes either. An alternative way to do this would be to use io-ts instead. There you will find the UnknownRecord.

Related