[Typescript] "Readonly" Mapped Type
typescript quot type readonly mapped
2023-09-14 08:59:13 时间
interface Person { name: string; age: number; } interface ReadonlyPerson { readonly name: string; readonly age: number; } const person: ReadonlyPerson { name: "Wan" age: 28 }
This works.
There is a better way:
interface Person { name: string; age: number; } const person:Readonly<Person> { name: "Wan" age: 28 }
What it does, is mapping over each prop in Person and add readonly midifier to it.
We can write one by our own:
type MyReadonly<T> = { readonly [P in keyof T]: T[P] }
freeze funtion:
function freezePerson<T> (obj: T): Readonly<T> { return Object.freeze(obj); }
相关文章
- [Typescript] Indexing an Object with Branded Types
- [Typescript 4.9] TypeScript 4.9: satisfies operator
- [Typescript] @typescript-eslint/unbound-method
- [Typescript] 114. Hard - Camelize
- [Typescript] Function scope in typescript
- [Typescript] Making TypeScript Stick - 6 - infer, build a ConstructorArg Type
- [TypeScript] Make typescript stick - 2
- [React Typescript 2022] Refactor a React Component using TypeScript
- [Typescript] Extend Functionality of a TypeScript Class with Decorators
- [TypeScript] "Pick" Mapped Type
- [Typescript] Make your optional fields required in TypeScript
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
- [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS
- [Typescript] Function overload :The "Instantiated with Subtype" Error
- [Typescript] 133. Medium - All
- [Typescript] Function scope in typescript
- [Typescript] TypeScript module Augmentation
- [Typescript + React] Tip: Use generics in React to make dynamic and flexible components
- [Typescript] Making TypeScript Stick - 5 - Extends
- [Typescript] Dictionary map, filter and reduce
- [TypeScript] ReturnType
- [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
- [Typescript] Use the Nullish Coalescing Operator in TypeScript (isNil)
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [TypeScript] "Pick" Mapped Type
- [Typescript] "Readonly" Mapped Type
- [Typescript] Enforcing Code Quality
- [Typescript] Make your optional fields required in TypeScript
- [Typescript] Exclude Properties from a Type in TypeScript (Omit)
- [TypeScript] Shallow copy object by using spread opreator
- [TypeScript] type inference
- TypeScript里的高阶函数-返回函数的函数,以及对应的JavaScript代码
- SAP Spartacus TypeScript和编译后的JavaScript命名规范