00007-easy-readonly

Back

type MyReadonly<T extends Record<string, any>> = { readonly [K in keyof T]: T[K] }

Solution by vaclock #35269

type MyReadonly<T> = {
  readonly [Property in keyof T]: T[Property]
}

Solution by Muntazir-sd #35193

/* 7 - Readonly

by Anthony Fu (@antfu) #easy #built-in #readonly #object-keys

Question

Implement the built-in Readonly<T> generic without using it.

Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.

For example:

interface Todo {
  title: string
  description: string
}

const todo: MyReadonly<Todo> = {
  title: "Hey",
  description: "foobar"
}

todo.title = "Hello" // Error: cannot reassign a readonly property
todo.description = "barFoo" // Error: cannot reassign a readonly property

View on GitHub: https://tsch.js.org/7 */

/* _____________ Your Code Here _____________ */

type MyReadonly = { readonly [K in keyof T]: T[K]; }

/* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils'

type cases = [ Expect<Equal<MyReadonly, Readonly>>, ]

interface Todo1 { title: string description: string completed: boolean meta: { author: string } }

/* _____________ Further Steps _____________ / /

Share your solutions: https://tsch.js.org/7/answer View solutions: https://tsch.js.org/7/solutions More Challenges: https://tsch.js.org */

Solution by leetflow #35154

type MyReadonly<T> = { readonly [P in keyof T] : T[P]}

Solution by JAGORING #35145

type MyReadonly<T extends object> = {
	readonly [P in keyof T]: T[P]
}

Solution by lwfxzz #35117

type MyReadonly<T extends object> = {
  readonly [key in keyof T] : T[key]
}

Solution by chenweiCwCw #35079

type MyReadonly<T> = {
  readonly [k in keyof T]: T[k]
}

Solution by koplec #35043

type MyReadonly<T> = {
  +readonly [K in keyof T]: T[K];
};

Define a new type by adding the readonly keyword to all properties of the generic type T.

Solution by miju-Park #34948

type MyReadonly<T> = {
  readonly [P in keyof T]: T[P];
};

Solution by raeyoung-kim #34889


type MyReadonly<T> = {
  readonly [key in keyof T]: T[key]
}

Solution by floatDreamWithSong #34868

type MyReadonly<T> = {
  readonly[key in keyof T] : T[key]
}

Solution by 56aiden90 #34866

type MyReadonly<T> = {
    readonly [K in keyof T]: T[K]
}

Solution by HrOkiG2 #34827

// your answers
type MyReadonly<T> = { readonly [K in keyof T]: T[K] }

Solution by eunsukimme #34807

type MyReadonly<T> = {
  readonly [P in keyof T]: T[P]
}

Solution by heryTz #34795

๋ฌธ์ œ ์„ค๋ช…

T์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ๊ธฐ ์ „์šฉ(์žฌํ• ๋‹น ๋ถˆ๊ฐ€)์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋‚ด์žฅ ์ œ๋„ค๋ฆญ Readonly<T>๋ฅผ ์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„ํ•˜์„ธ์š”.

interface Todo {
  title: string;
  description: string;
}

const todo: MyReadonly<Todo> = {
  title: "Hey",
  description: "foobar",
};

todo.title = "Hello"; // Error: cannot reassign a readonly property
todo.description = "barFoo"; // Error: cannot reassign a readonly property

readonly ์†์„ฑ ?

readonly๋Š” ํƒ€์ž… ๊ฐ์ฒด์˜ key ์•ž์— ๋ถ™์—ฌ์ฃผ๋ฉด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์ด๋ฏ€๋กœ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.

๊ตฌ์ฒด์ ์œผ๋กœ ์ดˆ๊ธฐํ™”๋Š” ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋ณ€๊ฒฝ์ด ์•ˆ๋œ๋‹ค.

interface Todo {
  readonly title: string;
  readonly description: string;
}

const firstTodo: Todo = {
  title: "typescript",
  description: "study",
};

firstTodo.title = "javascript"; // error

ํ’€์ด

Readonly ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ๋ฌธ์ œ์ด๋ฏ€๋กœ ๊ฐ€์ ธ์˜จ ํƒ€์ž…์„ T, ํ•ด๋‹น ํƒ€์ž… ๊ฐ์ฒด์˜ ํ‚ค๋ฅผ ์œ ๋‹ˆ์˜จํ˜•ํƒœ๋กœ ์ถ”์ถœํ•˜๊ณ  ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด in๊ณผ keyof ํ‚ค์›Œ๋“œ๋ฅผ ํ™œ์šฉํ•œ ๋งต๋“œํƒ€์ž…์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

type MyReadonly<T> = {
  readonly [K in keyof T]: T[K];
};

Solution by MyeonghoonNam #34788

type MyReadonly<T> = {
  readonly [t in keyof T] : T[t]
}

Solution by Kim-Ji-Seop #34786

type MyReadonly<T> = {
  readonly [K in keyof T]: T[K]
}

Solution by Oyeins-GUI #34780

type MyReadonly<T> = {
  readonly [P in keyof T] : T[P]
}

Solution by kang-kibong #34741

type MyReadonly<T> = {
  readonly [Property in keyof T] : T[Property];
}

Solution by lephuthuc2001 #34696

type MyReadonly<T> = {
  readonly [K in keyof T]: T[K];
};

Solution by raeyoung-kim #34680

type MyReadonly<T> = {
  readonly [key in keyof T]: T[key]
}

Solution by Skadelig #34671

type MyReadonly<T> = {
  readonly [key in keyof T]: T[key]
}

่ฟญไปฃT็š„key็ป„ๆˆ็š„่”ๅˆ็ฑปๅž‹๏ผŒๅนถ่ฟฝๅŠ ๅช่ฏปๆ“ไฝœ็ฌฆ

Solution by oOzhuoxiaOo #34670

// ไฝ ็š„็ญ”ๆกˆ

type MyReadonly = { readonly [P in keyof T]: T[P]; };

Solution by zeyuanHong0 #34666

type MyReadonly<T> = { readonly [S in keyof T]: T[S] }

Solution by nathan2slime #34653

type MyReadonly<T> = {
  readonly [x in keyof T]: T[x]
}

const data:MyReadonly<Todo1> = {
  title: "shinn thant",
  description:"This is description",
  completed:true,
  meta: {
    author:"awesome"
  }
}

Solution by devshinthant #34516

type MyReadonly<T> = {
  readonly[key in keyof T] : T[key]
}

Solution by JayoonKoo #34514

type MyReadonly<T> = {
  readonly [K in keyof T]: T[K]
}

Solution by ktim816 #34418

// your answers
type MyReadonly<T> = {
  readonly [K in keyof T] : T[K]
}

Solution by gobielJonathan #34393

// ไฝ ็š„็ญ”ๆกˆ
type MyReadonly<T> = {
  readonly[k in keyof T]: T[k]
}

่งฃๆž๏ผš้€š่ฟ‡ๆ˜ ๅฐ„็ฑปๅž‹็š„readonlyไฟฎ้ฅฐ็ฌฆๅฏ่ฟ›่กŒๅฏนๅบ”็š„ๅฎž็Žฐ

Solution by ICE-XIAOWU #34381

type MyReadonly<T> = {
  readonly [Key in keyof T] : T[Key]
}

Solution by YeongseoYoon #34375