Skip to content

bug: functions dislike iteration? #17086

Closed

Description

@KiaraGrouwstra

TypeScript Version: 2.4.1

Code

export type TupleHasIndex<
    Arr extends any[],
    I extends number
> = ({[K in keyof Arr]: '1' } & Array<'0'>)[I];
// ^ #15768, TS2536 `X cannot be used to index Y` on generic
export type Inc = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
export type TupleToUnion<R extends any[], I extends number = 0, Acc = never> =
    { 1: TupleToUnion<R, Inc[I], Acc | R[I]>, 0: Acc }[TupleHasIndex<R, I>];
type TupleToUnionTest1 = TupleToUnion<['a', 'b']>;
// "a" | "b"
type TupleToUnionTest2 = TupleToUnion<string[]>;
// never

declare function toUnion<R extends string[]>(arr: R): TupleToUnion<R>;
const test1 = toUnion(['a', 'b']);
const arr: ['a', 'b'] = ['a', 'b'];
const test2 = toUnion(arr);
// ^ both: expecting 'a'|'b', got never

Expected behavior:
'a'|'b'

Actual behavior:
never

I'd reported a similar failure at #12290 (comment), though the thread had already been closed. These are possibly related to #16018, of generic types similarly failing when wrapped, even without functions.

Activity

  1. KiaraGrouwstra commented on Aug 25, 2017

    @KiaraGrouwstra
    ContributorAuthor

    I retested this again on master now, where test2 does in fact work. Closing as no longer relevant until I can find a new repro for master.

  2. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions