import * as t from "io-ts";
interface Test {
toString: null | string;
prop?: unknown;
}
// This works:
const Test1: t.Type<Test> = t.type({
toString: t.union([t.null, t.string]),
});
// This does not work:
const Test2: t.Type<Test> = t.intersection([
t.type({
toString: t.union([t.null, t.string]),
}, "Req"),
t.partial({
prop: t.unknown,
}, "Opt"),
], "Test2");
The error is:
Type 'IntersectionC<[TypeC<{ toString: UnionC<[NullC, StringC]>; }>, PartialC<{ prop: UnknownC; }>]>' is not assignable to type 'Type<Test, Test, unknown>'.
Types of property 'encode' are incompatible.
Type 'Encode<{ toString: string | null; } & { prop?: unknown; }, { toString: string | null; } & { prop?: unknown; }>' is not assignable to type 'Encode<Test, Test>'.
Type 'Test' is not assignable to type '{ toString: string | null; } & { prop?: unknown; }'.
Types of property 'toString' are incompatible.
Type 'string | null' is not assignable to type 'string & (() => string)'.
Type 'null' is not assignable to type 'string & (() => string)'.
Type 'null' is not assignable to type 'string'.
Iām not actually sure if itās a bug in TS or in io-ts, see below. I could figure out how to make a short example using io-ts but not without it. Hopefully someone who understands the io-ts type hierarchies better than I do can help.
Additional context
The code used to work up to TypeScript 4.8.4, and it no longer works in 4.9.5 (nor in 5.0.4). I canāt tell if TypeScript introduced a bug, or if some io-ts type extends something it shouldnāt and some change in the type declarations or in the inference rules exposed this.
Note that this specifically happens only if the property is named toString, so presumably it is caused by some interference with Object.toString.
Your environment
I canāt figure out how www.typescriptlang.org picks versions, but locally I get the buggy behavior with the versions below:
š Bug report
Current Behavior
The following code fails typechecking:
The error is:
Expected behavior
I would expect the code to just work.
Reproducible example
Please see this playground.
Suggested solution(s)
Iām not actually sure if itās a bug in TS or in
io-ts
, see below. I could figure out how to make a short example usingio-ts
but not without it. Hopefully someone who understands theio-ts
type hierarchies better than I do can help.Additional context
The code used to work up to TypeScript 4.8.4, and it no longer works in 4.9.5 (nor in 5.0.4). I canāt tell if TypeScript introduced a bug, or if some
io-ts
type extends something it shouldnāt and some change in the type declarations or in the inference rules exposed this.Note that this specifically happens only if the property is named
toString
, so presumably it is caused by some interference withObject.toString
.Your environment
I canāt figure out how
www.typescriptlang.org
picks versions, but locally I get the buggy behavior with the versions below: