Search code examples
typescripttypesreturn-typetyping

Typescript: Enforce inner type, but return actual type


I have the following code:

class ValueGetter {
  getValues() {
    return {
      value1: this.getValueAsBoolean(somewhere.something),
      value2: this.getValueAsBoolean(somewhere.somethingElse)
    }
  }
}

Typescript knows the exact return-type of the function, which is.

{
  value1: boolean,
  value2: boolean
}

What's really handy that I can even do the following somewhere else:

class MyFoo {
  myBar: ReturnType<ValueGetter['getValues']>
}

Now what I want is to enforce getValues so it can only return the following type {[key: string]: boolean}

But if I add this as the return-type of getValues(), I loose the exact return-type with named keys which Typescript deduced for me.

Is there a way to enforce this typing only for the 'inside' of getValues, without loosing the cool deducement-magic with the 'named keys'-returntype Typescript can do?


Solution

  • Even though this question is answered here's a better way of doing what you want without adding any runtime overhead:

    type HasType<T extends C, C> = T;
    function getValues() {
        let foo = {
            value1: Boolean(100),
            value2: 123
        };
        return <HasType<typeof foo, Record<string, boolean>>>foo;
        //              ^^^^^^^^^^ error here
    }
    

    Here's a demo

    (Also consider marking this as accepted if you think this is better)