Skip to content

isArray not narrowing a type variable correctly since 5.8Β #62238

@jakebailey

Description

@jakebailey

πŸ”Ž Search Terms

isarray 5.8

πŸ•— Version & Regression Information

  • This changed between versions 5.7 and 5.8

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.2#code/KYDwDg9gTgLgBAYwgOwM72MgrgWzgYQEMZgBzaATzgG8BYAKDibh2BkIBNjC4BeOAEQB9Ia3Zd2AgDQNmcGBDAAbYADdgSvoIXK1G6bOYwAlqyXHkwLQI5qFEJagB0JsxeAHGzBGCwAFKAgAM2MVKGtVAA4nHywhMECQsM85DmNUQgAjFQ4AIQoAEWAgwiwlGCFXDXdrNIzs4A4AWkyKJtsSsph2uwgHZyrzSxTmOqyc-KLO8qFYgODQ4HD+G3Txxpa2jtLypqiY33jExahPAF8GBlBIWHkKMCsiEnIoY2BUAB58OFASZA5UARiGRKFoniCoBQAHx8QxMAA+cAAFEj0K9kKQ4IjVBBjBwAJRwABkNDgAG0BHBZhAcDhCABlYBgQhQYEccEvCgCAC6AC4CABuOBnfFwrGCy70K7gaDwIJYZAIEwoOAAK1xyA50Den2+v0wAKBz0oUKRCGBLx1-PwZO5+OtFu17y+MLoXiYUDYWCgyEQjte7yc6osSOkAkJhEBWoDuqhAoYFyl9Gusrg8sVyt9CmjOrN-qtRohOvFns4KCUVGjFFt9sLlveNDFnpg3t9AEEoKyKE50h2u3njTHRe65HAAPxqjU594DovvYej5j882DnXx+iJoA

πŸ’» Code

export const enum Category {
    metadata = "__metadata",
    toplevel = "toplevel",
    timeline = "devtools.timeline",
    cpuProfiler = "v8.cpu_profiler",
    disabledByDefault_timeline = "disabled-by-default-devtools.timeline",
    disabledByDefault_cpuProfiler = "disabled-by-default-v8.cpu_profiler",
}

export type Categories<C extends Category = Category> =
    | ((string | void) & { [" _commaSeparatedCategory"]: C; })
    | C;


export function joinCategories<C extends Category>(categories: C[]): Categories<C> {
    return categories.join(",") as Categories<C>;
}

export function toCategories(categories: Categories | readonly Category[]): Categories {
    return Array.isArray(categories)
        ? joinCategories(categories)
        : categories;
}

πŸ™ Actual behavior

5.8 and 5.9 say narrow categories to any[] instead of readonly Category[], and the else side is still the union, leading to an error.

πŸ™‚ Expected behavior

Categories should be the right array type, and the else branch should just be Categories.

Additional information about the issue

Noticed this while updating typescript-benchmarking past 5.7.

I think I remember an Anders PR in 5.8 that is related to this, but I can't remember what it is anymore.

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