ts-toolbelt icon indicating copy to clipboard operation
ts-toolbelt copied to clipboard

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

Open DetachHead opened this issue 4 years ago • 0 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 https://github.com/microsoft/TypeScript/issues/44268#issuecomment-944037476

DetachHead avatar Oct 16 '21 06:10 DetachHead