<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>minsu_32.log</title>
        <link>https://velog.io/</link>
        <description></description>
        <lastBuildDate>Tue, 22 Aug 2023 16:17:05 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>minsu_32.log</title>
            <url>https://velog.velcdn.com/images/minsu_32/profile/d8601aa3-b4ae-4250-ba36-e0a348a07af2/social_profile.jpeg</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. minsu_32.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/minsu_32" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[TS ValueOf<T> 구현방법]]></title>
            <link>https://velog.io/@minsu_32/TS-ValueOfT-%EA%B5%AC%ED%98%84%EB%B0%A9%EB%B2%95</link>
            <guid>https://velog.io/@minsu_32/TS-ValueOfT-%EA%B5%AC%ED%98%84%EB%B0%A9%EB%B2%95</guid>
            <pubDate>Tue, 22 Aug 2023 16:17:05 GMT</pubDate>
            <description><![CDATA[<pre><code class="language-typescript">const UseAsConst = {
    준비 : 1,
    진행중 : 2,
    done : 3,
    cancel : 8,
    etc: 0,
} as const

const enum UseEnum {
    준비 = 1,
    진행중 = 2,
    done = 3,
    cancel = 8,
    etc= 0,
} 

//keyof typeof만 사용한 방식
type UseAsConstValueOf = (typeof UseAsConst)[keyof typeof UseAsConst]

type UseEnumValueOf = (typeof UseEnum)[keyof typeof UseEnum]

//제네릭 방식
type valueOf&lt;T&gt; = T[keyof T];

type User = {
    id: number,
    name: string,
    state: UseAsConstValueOf //valueOf&lt;typeof UseEnum&gt; | UseEnumValueOf
}

const kim: User = {id:1,name:&quot;minsu&quot;, state:1};
console.log(kim);</code></pre>
<p>ts에서 enum은 tsc에서 js로 변환시 즉시실행함수로 (preserveConstEnums 옵션을 줌으로 즉시실행함수로 번들링 안되게 할 수 있음) 번들링되고 다른 블로그에 많은 글처럼 Tree-shaking이 되지 않는다.
as const쓰면 속성이 readonly로 되어 삽입수정이 불가하고 각 속성의 타입을 리터럴 타입으로 추론한다.
암튼 enum관련된 글은 블로그에 많으니 다른 블로그에 글도 많고, 찾아보는걸 추천한다.</p>
<p><strong>본론으로 돌아와서 Typescript에서 keyof는 있는데 valueOf는 없어서 제네릭을 이용한 방법과 typeOf와 keyOf를 구현 방법을 정리했다</strong></p>
]]></description>
        </item>
    </channel>
</rss>