Skip to content
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

Methods with generic properties in a class cannot be assigned to unknown. #58108

Closed
LiST-GIT opened this issue Apr 8, 2024 · 1 comment
Closed

Comments

@LiST-GIT
Copy link

LiST-GIT commented Apr 8, 2024

πŸ”Ž Search Terms

generic
method

πŸ•— Version & Regression Information

v5.4.4

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAECCA8AVAfNA3gWAFDV9AJgKahgBOh0AZgK4B2wAXNABQBuYI1hTiAlNAF5UrAPYBLfAG5seaMA4hm-TDll5gI2hAAu0MEwR0A1rREB3WqgHRtACzERpq3AF9sbrEA

πŸ’» Code

class A<T> {
    declare func: (value: T) => void;
    call() {
        const a: A<unknown> = this;
           // ^
    }
}
Type 'this' is not assignable to type 'A<unknown>'.
  Type 'A<T>' is not assignable to type 'A<unknown>'.
    Type 'unknown' is not assignable to type 'T'. // <-----
      'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.

πŸ™ Actual behavior

Judging from the error message, could it be that the comparison was written in reverse?

πŸ™‚ Expected behavior

Any value can be assigned to unknown.

Additional information about the issue

No response

@jakebailey
Copy link
Member

jakebailey commented Apr 8, 2024

This error is correct. Say you have an A<number>. It must be the case that you can only pass numbers into its func. But if you assign an A<number> to A<unknown>, now func can accept unknown, which is anything, which violates what func of A<number> expects.

@LiST-GIT LiST-GIT closed this as completed Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants