Given a function and its variables' types, how to get the type of the return value?

33 Views Asked by At

Say we have this example:

const a = 'Hello' as const 
function f(){
    return a[0]
} 
const b = f()

The type of b is string, not 'H'. Is there a way to make it so? In practice, I want something like this:

type A = 'Hello'
type B = f(A)

So that if I need to change A, I don't need to change B too. I read about Generics but still don't know how to apply it. Getting first character in the string is just an example. The input variables can be arrays, objects, etc.

0

There are 0 best solutions below