Skip to content

never assignability check disappears for method call from generic functionΒ #57453

Closed
@rotu

Description

@rotu

πŸ”Ž Search Terms

never bivariant method assignable generic

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about methods

⏯ Playground Link

https://www.typescriptlang.org/dev/bug-workbench/?ts=5.4.0-dev.20240220#code/C4TwDgpgBAYg9nAPAFQHxQLxQN4CgoFQCGAshMABZwAmAFMAFzICUDAbnAJbUA0+hAegFQAzsABORAHYBzCABsQPUZwC2YRVACuUgMZxVqiFOCdZUStCkQAHsCjyz0XUS0iIIixWgAjBXAB3KFtIXWAIamDxcThxCzgocQgiMEgicQBCfgIhYhgdMM44KQZ6JmYMVA5uXABfXFxqCF15dOdisSg4HwArBngkazYIcVRcbp6AOlJyKjoARmYoXIBBcRktIxMugDMLcGgAcilNv3FDqE5PKTh7IhERThkpIh95aGAEsHSiI3C4uB7UCQKDHCDDc64XJNSBSaieYpQfSGYzhSKOaxQ4QTab5PSmYq0RYNfRSToueTyMiUGiYKAoHioWhEfoIFCoZQ+cqVPCEYjTalzWg+Zh1ElESmCmi0CbKRbLYQhZpoqIxcS4IA

πŸ’» Code

type Foo<T> = {
    aMethod(t:T):void,
    // strangely, simply uncommenting the next line causes the below expected error to reappear!
    // aFunction:(t:T)=>void
}

declare const obj:Foo<never>
obj.aMethod(1) // Argument of type 'number' is not assignable to parameter of type 'never'
// depends on commented line
// obj.aFunction(1)

const callMethod = <T,>(a:Foo<T>, b:T)=>{
    a.aMethod(b)
}

callMethod(obj, 1) // expected error

πŸ™ Actual behavior

callMethod does not issue a type warning that "Argument of type 'number' is not assignable to parameter of type 'never'".

Doing the same call outside a generic function body causes the error again.

πŸ™‚ Expected behavior

I expect a warning, similar to if I had called the method directly or if the method were instead a function.

Additional information about the issue

Note this was originally discovered in the context of Array<never> which, when used in a generic function, allows one to unsafely push any type T: #57419 (comment).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions