Below is a code snippet that raises the question of how one should define certain types. In this scenario, it is required that Bar
extends Foo
and the return type of FooBar
should be 'a'
.
interface Foo {
(...args: any):any
b: string
}
interface Bar<T extends string> extends Foo {
(a:T):T
b: T
}
// The type of BarA below should be:
// {
// (a:'a'):'a'
// b: 'a'
// } - Does this seem correct?
type BarA = Bar<'a'>
type FooBarWorks = BarA['b'] // Output expected as 'a'
type FooBar = ReturnType<BarA> // Why does this result in 'any' instead of 'a'?
Could someone provide an explanation as to why this code does not function as anticipated?
Thank you.
EDIT: The following solution addresses the issue:
type Bar<T extends string> = {
(a:T):T
b: T
} extends infer O extends Foo ? O : never
But what is the reason for the interface solution mentioned above not working?