首页 > 其他分享 >[Typescript] 17. Medium - Readonly 2

[Typescript] 17. Medium - Readonly 2

时间:2022-09-08 02:33:12浏览次数:100  
标签:Typescript string completed readonly Readonly keyof Medium type MyReadonly2

Implement a generic MyReadonly2<T, K> which takes two type argument T and K.

K specify the set of properties of T that should set to Readonly. When K is not provided, it should make all properties readonly just like the normal Readonly<T>.

For example

interface Todo {
  title: string
  description: string
  completed: boolean
}

const todo: MyReadonly2<Todo, 'title' | 'description'> = {
  title: "Hey",
  description: "foobar",
  completed: false,
}

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

 

/* _____________ Your Code Here _____________ */
// 1. Make all the props as readonly
// 2. & the prop not in K without readonly
// 3. K: set default value as keyof T, and should extends keyof T
type MyReadonly2<T, K extends keyof T = keyof T> = {
  readonly [P in keyof T]: T[P]
} & {
  [P in keyof T as P extends K ? never: P]: T[P]
}

type x = keyof Todo2
/* _____________ Test Cases _____________ */
import type { Alike, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Alike<MyReadonly2<Todo1>, Readonly<Todo1>>>,
  Expect<Alike<MyReadonly2<Todo1, 'title' | 'description'>, Expected>>,
  Expect<Alike<MyReadonly2<Todo2, 'title' | 'description'>, Expected>>,
]

// @ts-expect-error
type error = MyReadonly2<Todo1, 'title' | 'invalid'>

interface Todo1 {
  title: string
  description?: string
  completed: boolean
}

interface Todo2 {
  readonly title: string
  description?: string
  completed: boolean
}

interface Expected {
  readonly title: string
  readonly description?: string
  completed: boolean
}

 

Step by step:

1. Make all prop as readonly

type MyReadonly2<T, K extends keyof T = keyof T> = {
  readonly [P in keyof T]: T[P]
} 

type x = MyReadonly2<Todo1, 'title' | 'description'>

/*
type x = {
    readonly title: string;
    readonly description?: string | undefined;
    readonly completed: boolean;
}
*/

2. For prop not in K, make as not readonly

type MyReadonly2<T, K extends keyof T = keyof T> = {
  [P in keyof T as P extends K ? never: P]: T[P]
}

type x = MyReadonly2<Todo1, 'title' | 'description'>
/*
type x = {
    completed: boolean;
}
*/

3. Do intersection:

type MyReadonly2<T, K extends keyof T = keyof T> = {
  readonly [P in keyof T]: T[P]
} & {
  [P in keyof T as P extends K ? never: P]: T[P]
}

 4. K extends keyof T = keyof T set default value of K as keyof T if not defined and K should extends keyof T

标签:Typescript,string,completed,readonly,Readonly,keyof,Medium,type,MyReadonly2
From: https://www.cnblogs.com/Answer1215/p/16667924.html

相关文章

  • Typescript类型体操 - kebab case
    题目中文将camelCase或PascalCase的字符串转换为kebab-case的风格示例:typeFooBarBaz=KebabCase<'FooBarBaz'>;constfoobarbaz:FooBarBaz='foo-bar-baz'......
  • Typescript类型体操 - String to Union
    题目中文实现一个将接收到的String参数转换为一个字母Union的类型。例如typeTest='123';typeResult=StringToUnion<Test>;//expectedtobe"1"|"2"|......
  • Typescript类型体操 - Absolute
    题目中文实现一个接收string,number或bigInt类型参数的Absolute类型,返回一个正数字符串。例如typeTest=-100;typeResult=Absolute<Test>;//expectedtobe"......
  • Typescript类型体操 - Append to object
    题目中文实现一个为接口添加一个新字段的类型。该类型接收三个参数,返回带有新字段的接口类型。例如:typeTest={id:'1'}typeResult=AppendToObject<Test,'va......
  • Typescript类型体操 - Flatten
    题目中文在这个挑战中,你需要写一个接受数组的类型,并且返回扁平化的数组类型。例如:typeflatten=Flatten<[1,2,[3,4],[[[5]]]]>//[1,2,3,4,5]EnglishIn......
  • Typescript类型体操 - Length of String
    题目中文计算字符串的长度,类似于String#length。EnglishComputethelengthofastringliteral,whichbehaveslikeString#length.答案解法1typeStringToArr......
  • [Typescript Challenges] 15. Medium - Omit
    Implementthebuilt-in Omit<T,K> genericwithoutusingit.Constructsatypebypickingallpropertiesfrom T andthenremoving KForexampleinterfaceT......
  • Typescript类型体操 - Append Argument
    题目中文实现一个泛型AppendArgument<Fn,A>,对于给定的函数类型Fn,以及一个任意类型A,返回一个新的函数G。G拥有Fn的所有参数并在末尾追加类型为A的参数。typeF......
  • Typescript类型体操 - Parameters
    题目中文实现内置的Parameters<T>类型,而不是直接使用它,可参考TypeScript官方文档。例如:constfoo=(arg1:string,arg2:number):void=>{}typeFunctionParams......
  • Typescript类型体操 - ReplaceAll
    答案中文实现ReplaceAll<S,From,To>将一个字符串S中的所有子字符串From替换为To。例如typereplaced=ReplaceAll<'types','',''>//期望是'types'......