[Typescript Challenges] 2. Easy -- readonly
typescript -- Easy readonly
2023-09-14 08:59:12 时间
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
/* _____________ Your Code Here _____________ */
type MyReadonly<T> = {
readonly [K in keyof T]: T[K]
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<MyReadonly<Todo1>, Readonly<Todo1>>>,
]
interface Todo1 {
title: string
description: string
completed: boolean
meta: {
author: string
}
}
相关文章
- [Typescript] Merge Object into Window with Types
- [Typescript] 92. Medium - PathParams
- [Typescript] Get a TypeScript package ready for release to NPM
- [Typescript] Making TypeScript Stick - 6 - infer, build a ConstructorArg Type
- [TypeScript] ReturnType
- [Typescript] Use the Optional Chaining Operator in TypeScript
- [Typescript] Extend Functionality of a TypeScript Class with Decorators
- [Typescript] “keyof”, Generics and Lookup Types
- [Typescript Kaop-ts] Use AOP in Vue Components with TypeScript and Kaop-ts
- [Typescript] Creating Chainable Method Abstractions with Generics and the Builder Pattern - 05
- [Typescript] Declaring Globals for a Library
- [Typescript] Extract & Exclude
- [TypeScript] Model a DataStore type
- [React Typescript 2022] Type a Radio Button Component in React using TypeScript
- [React Typescript 2022] Refactor a React Component using TypeScript
- [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
- [Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
- [Typescript] Statically Type Unknown Values with TypeScript's unknown Type
- [Typescript] Use the Nullish Coalescing Operator in TypeScript (isNil)
- [Typescript] Use the Optional Chaining Operator in TypeScript
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [TypeScript] Modifier
- [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS
- 使用 TypeScript 自定义装饰器给类的属性增添监听器 Listener