millsp / ts-toolbelt

πŸ‘· TypeScript's largest type utility library

Home Page:https://millsp.github.io/ts-toolbelt/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`Narrow` doesn't work on `this` parameters

DetachHead opened this issue Β· comments

🐞 Bug Report

Reproduce the bug

declare global {
    interface Array<T> {
        fn<A>(this: Narrow<A>): A
    }
}

// expected type: ['foo', 'bar']
// actual type: string[]
const foo = ['foo', 'bar'].fn()

Expected behavior

type is ['foo', 'bar']

Additional context

i'm trying to improve some of the builtin types using ts-toolbelt, but can only do so using a this parameter

declare global {
    interface ReadonlyArray<T> {
        join<Arr extends ReadonlyArray<Literal>, D extends string = ','>(
            this: Arr,
            delimiter?: D
        ): Join<Arr, D>
    }
    interface Array<T> extends ReadonlyArray<T> {
        join<Arr extends ReadonlyArray<Literal>, D extends string = ','>(
            this: Arr,
            delimiter?: D
        ): Join<Arr, D>
    }
}

see microsoft/TypeScript#44268 (comment)