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
by Anthony Fu (@antfu) #easy #built-in #readonly #object-keys
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
/* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<MyReadonly
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๋ ํ์ ๊ฐ์ฒด์ 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
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