gcanti / io-ts

Runtime type system for IO decoding/encoding
https://gcanti.github.io/io-ts/
MIT License
6.68k stars 331 forks source link

ReadonlyNonEmptyArray can't be used in a mapped type #691

Closed matulek closed 1 year ago

matulek commented 1 year ago

It's probably caused by the type intersection with { readonly 0: T } in here:

/**
 * @category model
 * @since 2.5.0
 */
export declare type ReadonlyNonEmptyArray<A> = ReadonlyArray<A> & {
  readonly 0: A
}

The example of a problematic code:

import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray';

type MyType<T extends string> = null;
type MappedType<T extends ReadonlyNonEmptyArray<string>> = {
    [K in keyof T]: MyType<T[K]>;
};

the problem is at line 5 with a generic argument T[K]. The error message is:

Type 'T[K]' does not satisfy the constraint 'string'. Type 'T[keyof T]' is not assignable to type 'string'. Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'string'. Type 'T[string]' is not assignable to type 'string'.

I assume that TypeScript finds a possibility of T[string] because of the object-like fragment in the intersection, but that's just a blind guess of me because I'm not quite a TS ninja. To be honest I'm not entirely sure that I'm not doing something wrong and I have no idea if any solution to this problem exists.

matulek commented 1 year ago

Wrong repo, closing this one.