I have a function with an overload:
interface FunctionWithOverload {
(): {
a: 1
b: 1
}
(arg: T): {
a: 1
I've managed (with the help of TitianCernicova-Dragomir) to solve this! Like this:
interface FunctionWithOverload {
(): {
a: 1
b: 1
}
<T>(arg: T): Id<{
a: 1
b: 1
} & (T extends number ? { c: 1 } : {})>
}
type Id<T>={} & { [P in keyof T] :T[P]}
const fwo: FunctionWithOverload = () => {return {} as any}
const result = fwo() // result has a nice type: {a: 1, b: 1}
const result1 = fwo(1) // result1 DOES HAVE TOO!!! {a: 1, b: 1, c: 1}
Playground
I think the way it works probably is: the TS engine considers mapped types as worthy of being named types, like interfaces, while any unions, intersections (like in the solution above) or basic types like string literals are not.