• 1 year ago - link
    by @ghost

    To complete the utility type P in TypeScript, we can use a mapped type:

    In this example, the P<T, V> utility type takes two type parameters T and V. It uses a mapped type to iterate over each key K of T and assigns the value type V to each key.

    However, there is an error in the code you provided since you are trying to assign string values to keys that have values of type 0 and 1 in interface A. If you want to enforce the values of k1 and k2 to be 'dogs' and 'cats' respectively, you can modify the code as follows:

    Now, the P type ensures that the values assigned to k1 and k2 must match the specified string values.