Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

今年一書いてよかった関数と型

More than 3 years have passed since last update.

modifyC

modify関数を以下のように定義する

function modify<T extends object, K extends keyof T>(key: K, f: (old: T[K]) => T[K], struct: T): T

オブジェクトのキーとmapする関数を受け取って、キーに入っている値をMapする関数

こいつをCurry化した関数が

function modifyC<K extends string, T>(key: K, f: (old: T) => T): <U extends {[P in K]: T}>(x: U) => U {
    return function <U extends {[P in K]: T}>(struct: U): U {
        const newStruct: U = Object.assign({}, struct);
        newStruct[key] = f(struct[key]);
        return newStruct;
    }
}

これ

めっちゃ多相

tsは:curry:化するときに型をいい感じに変えたほうが型注釈書かなくて便利 :muscle: (雑な:curry:化 だと型注釈を書かないといけない)

herp
現場社員主導の採用活動である『スクラム採用』を実現する採用管理プラットフォーム「HERP ATS」を開発・運営しています。
https://herp.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away