Lookup type on `this` unexpectedly takes `true` branch of conditional type
See original GitHub issue- Asked on Gitter
- Tried my best to search existing issues
- Tried questioning my understanding, but still the behavior feels weird to me
- Finally, I am forced to create this issue (though I know, the issues list is too big)
TypeScript Version: v3.8.3
and Nightly
Search Terms: Generics within class body, Generics in class body
Code
type IsString<T extends any, K extends keyof T> = T[K] extends string ? T[K] : never
class Foo {
public username!: string
public age!: number
public dob!: { [key: string]: any }
public getStringAttr<T, K extends keyof T>(this: T, key: K): IsString<T, K> {
return {} as unknown as IsString<T, K>
}
public someInternalFunction() {
this.getStringAttr('dob')
}
}
new Foo().getStringAttr('dob')
The above is just a dummy code to reproduce the issue with minimum effort.
- The class
Foo
has a methodgetStringAttr
, which accepts the name of the class property and returns it back if it is a string, ornever
if it is not a string
Expected behavior:
Regardless of where the getStringAttr
method is called, it should have consistent behavior.
Actual behavior:
- Calling
getStringAttr
by creating a new class instance works fine - Calling
getStringAttr
from a different class method (within the class body) makes it infer incorrect types - The
getStringAttr('dob')
should returnnever
in both cases. But within the class body it returnsstring
. - The
getStringAttr('age')
should returnnever
in both cases and it does so.
Related Issues: Cannot find any
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:22 (4 by maintainers)
Top Results From Across the Web
Documentation - Conditional Types - TypeScript
Conditional types take a form that looks a little like conditional ... the type in the first branch (the “true” branch); otherwise you'll...
Read more >TypeScript lookup/conditional types and unions - Stack Overflow
I have a simple question: Is it possible to obtain a type of a part of a union in TypeScript?
Read more >Simulink Checks - MathWorks
Simulink software back-propagates dimensions, sample times and data types from downstream blocks unless you ... Conditional input branch execution.
Read more >Conditional (computer programming) - Wikipedia
In computer science, conditionals are programming language commands for handling decisions. ... a programmer-defined boolean condition evaluates to true or false.
Read more >Diagnostic flags in Clang — Clang 16.0.0git documentation
warning: taking the absolute value of unsigned type A has no effect ... warning: lookup of A in member access expression is ambiguous;...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe you are not aware of this, but this bug creates an issue in AdonisJS ORM. Many people are encountering this bug, not only one. We tell them to wait since we created this issue.
Some discussions about it:
How about moving the milestone to 4.7?