question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 method getStringAttr, which accepts the name of the class property and returns it back if it is a string, or never 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 return never in both cases. But within the class body it returns string.
  • The getStringAttr('age') should return never in both cases and it does so.

Playground Link: https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=17&pc=31#code/C4TwDgpgBAkgzgZWAJwJYDsDmAeAKlCAD2AnQBM4oBDdEAGigGkDjSKoBrCEAewDMouAHxQAvIIDajALosS5SnBQZMUAPySZUAFxR0EAG4RkAWABQ5gMYAbKnEoAxHjygBvc1ChgArgCNrqJZQ3nDG6FQAthAAhLpKaFgeXn4BQVSYMbro3hG+xkk+-oFQZDy+sW5QElwgccpY0ro0IFAAvuYFKcUZwEgJmACCwCh4DMxE8uw1-IJCABTAABaocLq4DDW6jACUuvB9KqNMIu5mnp7IEMDeyOhurdSU3ugc6DwA7nd2sIj1OOvHJLtCxnZJFIJwHhRGDoEi3KjWBzPSzAVA8dBzbZuJKeJYrAB0PQOWCGKDmAHJSr5ydsgeZgeZ9O8oE4eJjCVdiYNhsgKVSaUA

Related Issues: Cannot find any

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
RomainLanzcommented, Feb 24, 2021

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:

2reactions
thetutlagecommented, Apr 28, 2022

How about moving the milestone to 4.7?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found