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

Argument of type [type] is not assignable to parameter of type [typeof itself] #60508

Closed
lionel-rowe opened this issue Nov 15, 2024 · 2 comments

Comments

@lionel-rowe
Copy link
Contributor

🔎 Search Terms

Type narrowing, typeof, type casting, as, argument of type is not assignable to parameter of type typeof itself

🕗 Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAGlC8UDeUDWBLAdgEwFxQHIBDAqAH0ICNSBfAKDuwgGMAbIgJ2gDMBXTZsHQB7TFG6YAFJXwoMOfAWpQaASnwA3YemwMmbTtGaiAzsCgAPfDAbpuUSRYB087AniIlBVcjpR-4lIWqn4BEo5QRCZQoJDC9sGh-uEWkdGxEPGWUABkyGhYeDHgmQkuhSoh9EA

💻 Code

type X = { kind: 'a' | 'b' }

declare function fn(b: { kind: 'b' }): void

declare const x: X

if (x.kind === 'b') {
    // Argument of type 'X' is not assignable to parameter of type '{ kind: "b"; }'.
    fn(x)
    // Argument of type 'X' is not assignable to parameter of type '{ kind: "b"; }'.
    fn(x as typeof x)
    // ok
    fn(x as typeof x & { kind: typeof x.kind })
}

🙁 Actual behavior

First 2 versions give type errors.

🙂 Expected behavior

All 3 versions pass type checking.

Additional information about the issue

It seems like all the type info is available and inferred correctly by TS, given that the third version works, which makes it even weirder that the first two fail. How are x and x as typeof x different from x as typeof x & { kind: typeof x.kind }?

@jcalz
Copy link
Contributor

jcalz commented Nov 15, 2024

Duplicate of #42384

@lionel-rowe
Copy link
Contributor Author

Duplicate of #42384

@jcalz Thanks! I was sure someone would have raised it before but searching threw up nothing relevant. Closing as dupe.

@lionel-rowe lionel-rowe closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 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