1

This is what i am trying to accomplish but for an object with generics. Is there a `valueof` similar to `keyof` in TypeScript?.

// Something on this lines. I know this is not supported but i am trying to convey the idea here.

type payload<T> = <K extends keyof T>{prop: K, value: T[K]};

const someObj = {a: string, b: number};

type someObjType = payload<someObj>;

const someObjPayload: someObjType = { prop: 'a', value: 'some string'} // should work.

const someObjPayload: someObjType = { prop: 'a', value: 200 } // should throw an error.

2 Answers 2

1

Unfortunately typescript doesn't allow partial inference for variables. If you want to ensure that the value is of the same type as the property specified by key you will need o use a helper function to get the apropriate inference behavior:

type payload<T, K extends keyof T> = {prop: K, value: T[K]};

let someObj!: {a: string, b: number};

function getProp<K extends keyof typeof someObj>(o: payload<typeof someObj, K>) {
  return o
}

const someObjPayload = getProp({ prop: 'a', value: 'some string'}) // ok of type payload<{ a: string; b: number; }, "a">

const someObjPayload2= getProp({prop: 'a', value: 100}) // error
Sign up to request clarification or add additional context in comments.

Comments

1

Since only number and string are allowed in TypeScript, it might be simpler to just write everything two times - leaving your type parameter K out

type stringKeyPayload<T> = {prop: string, value: T}
type numberKeyPayload<T> = {prop: number, value: T}

3 Comments

Sorry for the lame answer :D I recently had a similar problem and came up with that solution
The problem with your answer is that value should be a type taht is a property type of T not T (ie T[keyof T]) And the problem OP wants to solve is to ensure the value is of the same type as the property specified by K ..
Ah, I see. Thanks for pointing that out. @Titian's answer looks way better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.