With TypeScript, can I type a curried version of getProperty<T, K extends keyof T>

丶灬走出姿态 提交于 2020-01-30 06:32:29

问题


Example from https://www.typescriptlang.org/docs/handbook/advanced-types.html

function getProperty<T, K extends keyof T>(o: T, name: K): T[K] {
    return o[name]; // o[name] is of type T[K]
}

Curried version:

function curriedGetProperty<T, K extends keyof T>(name: K): (o: T) => T[K] {
    return (o: T) => o[name]; // o[name] is of type T[K]
}

const record = { id: 4, label: 'hello' }

const getId = curriedGetProperty('id') // Argument of type '"id"' is not assignable to parameter of type 'never'.

const id = getId(record)

回答1:


type WithProp<T extends any, K extends string> = { [P in K]: T[P] }

function curriedGetProperty <P extends string>(prop: P) {
  return <T, O extends WithProp<T, typeof prop>>(o: O) => {
    return o[prop]
  }
}

Seems to type it safer.

const getId = curriedGetProperty('id')
getId({id: 'foo'}) // returns string
getId({label: 'hello'}) // fails



回答2:


If you split it into a two-step process, it can be minimally verbose and completely type safe at the same time:

interface recordType {
   id: number,
   label: string
}

const record = { id: 4, label: 'hello' };

const getPropertyBuilder = function <T>() {
   return <K extends keyof T>(key: K) => (o: T) => o[key];
};

const propertyBuilder = getPropertyBuilder<recordType>();
const getId = propertyBuilder('id'); // getId is (o: recordType) => number
const id = getId(record); // id is number

// or in one go
const label = getPropertyBuilder<recordType>()('label')(record); // label is string

Also works with Partial as mentioned:

const propertyBuilder = getPropertyBuilder<Partial<typeof record>>();
const getId = propertyBuilder('id');
const id = getId(record); // id is number
const id2 = getId({ id: 3 }); // also number



回答3:


const getProperty = <P extends string>(prop: P) => <O extends any>(obj: O) => obj[prop]

const record = { id: 4, label: 'hello' }

const getId = getProperty('id')

const id = getId(record)

This seems to work. The type for id is inferred properly as a number. Only thing is you'll receive any if the object passed into getId doesn't have an id property on it, so it's not strict, but an overall elegant solution.

EDIT: Since writing this answer, I've learned that the Record type can be used to specify a type of object which requires a specific key. Using this knowledge, we can write a typesafe, succinct, readable solution:

// implementation
const get = <K extends string>(key: K) => <V>(obj: Record<K, V>) => obj[key]

// usage
const person = {
  name: "kingdaro",
  age: 21,
}

const fruit = {
  type: "apple",
  color: "red",
}

const nameGetter = get("name")

nameGetter(person) // return type inferred as string
nameGetter(fruit) // fails, fruit has no key "name"

// minor caveat: when passing an object literal, the extra key will raise an error
// you can declare the object separately to sidestep around this
// but this wouldn't come up often anyway
nameGetter({ name: "kingdaro", age: 21 })



回答4:


Using TypeScript 3.0.3 I was able to do this:

function composeGetter<K extends string>(prop: K) {
    function getter<T extends { [P in K]?: any }>(object: T): T[typeof prop]
    function getter<T extends { [P in K]: any }>(object: T) {
        return object[prop]
    }

    return getter
}


来源:https://stackoverflow.com/questions/47847898/with-typescript-can-i-type-a-curried-version-of-getpropertyt-k-extends-keyof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!