Closed as not planned
Description
π Search Terms
symbol string number literal abstract narrow return class subclass
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
const num = 5
const str = "foo"
const sym = Symbol()
abstract class Base {
abstract getString(): "foo"
abstract getNumber(): 5
abstract getSymbol(): typeof sym
abstract getCastedSymbol(): typeof sym
}
class Child extends Base {
// Error: Type 'string' is not assignable to type '"foo"'
getString() {
return str
}
// Error: Type 'number' is not assignable to type '5'
getNumber() {
return num
}
// Error: Type 'symbol' is not assignable to type 'unique symbol'
getSymbol() {
return sym
}
// Error: Type 'symbol' is not assignable to type 'unique symbol'
getCastedSymbol() {
// Similarly casting str or num to typeof str or typeof num respectively does remove the error, so this seems to be unique to the symbol case
return sym as typeof sym
}
}
π Actual behavior
Return types were not contextually narrowed and allowed.
π Expected behavior
Return types are contextually narrowed and allowed. Currently the best workaround is to add an explicit return annotation. Symbol case is possibly related to #53276, associated PR is #54778