You can simply use the 'union' function and merge the DateFromISOString and string types.
const Foo = t.type({
id: t.number,
date: t.union([DateFromISOString, t.string]),
});
Another solution I found was this:
Build a Date instance from the string and then pass it to the string again using the 'toISOString' helper function.
const Foo = type({
id: number,
date: DateFromISOString,
});
type FooType = TypeOf<typeof Foo>;
const jsonFoo: FooType = { id: 1, date: new Date("2021-02-05T11:13:22.520Z") };
const resultFoo = Foo.decode(jsonFoo);
const input = jsonFoo.date.toISOString();
assert.deepStrictEqual(input, right(jsonFoo.date)); // fails
assert.deepStrictEqual(DateFromISOString.decode(input), right(jsonFoo.date)); // success
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…