If we consider the example provided, is there a way to instruct the typescript compiler that the return type of baz
must be string
, since it can be inferred from foo.a('aString')
that it's a string
?
const fn = <T,S>()=>{
let s: S
let t: T
return {
a: <Z extends T=T>(arg: Z)=>{
t = arg
return s
},
b: <V extends S=S>(arg:V)=>{
s = arg
return t
}
}
}
const foo = fn()
const bar = foo.a('aString')
// currently,baz's type is 'unknown', but it is determinable that the type should be 'string'
const baz = foo.b('aString')
A solution is to define
const foo = fn<string, string>()
. However, this requires one to specify both types upfront when foo.a('aString')
may be the best place to infer T
, rather than having to explicitly specify the type.
I suspect not ... but one can have hopium!