Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matulek opened this issue Mar 12, 2023 · 0 comments
Open

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

matulek opened this issue Mar 12, 2023 · 0 comments
Milestone

Comments

@matulek
Copy link

matulek commented Mar 12, 2023

馃悰 Bug report

Current Behavior

ReadonlyNonEmpty arrays are problematic when used in mapped types.

In the provided example 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'.

Expected behavior

There should be no error in the example I provided.

Reproducible example

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].

Suggested solution(s)

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
}

I assume that TypeScript finds a possibility of T[string] because of the object-like fragment in the intersection.

Probably the solution is identical to #1830 I've tested it and it works as expected in my scenario:

type ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];

Your environment

Which versions of fp-ts are affected by this issue? Did this work in previous versions of fp-ts?

Certainly my version, I have no idea whether or not it worked in previous versions but the problem is too straightforward to assume that it did.

Software Version(s)
fp-ts 2.13.1
TypeScript 4.2.2
@gcanti gcanti added this to the 3.0.0 milestone Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants