Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rotu opened this issue Feb 20, 2024 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@rotu
Copy link

rotu commented Feb 20, 2024

πŸ”Ž 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).

@rotu rotu changed the title never assignability check disappears for method never assignability check disappears for method call from generic function Feb 20, 2024
@RyanCavanaugh
Copy link
Member

This is just method parameter bivariance.

// Neither are errors
const a: Foo<never> = null! as Foo<1>;
const b: Foo<1> = null! as Foo<never>;

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 20, 2024
@rotu
Copy link
Author

rotu commented Feb 20, 2024

This is just method parameter bivariance.

// Neither are errors
const a: Foo<never> = null! as Foo<1>;
const b: Foo<1> = null! as Foo<never>;

Blech! I was so focused on the variance of aMethod that missed the variance of callMethod's parameters. You're right. callMethod gets instantiated with type 1 instead of never and obj get implicitly coerced from Foo<never> to Foo<1> in the function call.

I can see that this is fixed by instead annotating it: const callMethod = <T,>(a:Foo<T>, b:NoInfer<T>).

@rotu rotu closed this as completed Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants