首页 > 其他分享 >Typescript类型体操 - Mutable

Typescript类型体操 - Mutable

时间:2022-09-22 02:33:25浏览次数:88  
标签:Typescript string title readonly boolean 体操 Mutable description

题目

中文

实现一个通用的类型 Mutable<T>,使类型 T 的全部属性可变(非只读)。

例如:

interface Todo {
    readonly title: string;
    readonly description: string;
    readonly completed: boolean;
}
type MutableTodo = Mutable<Todo>; // { title: string; description: string; completed: boolean; }

English

Implement the generic Mutable<T> which makes all properties in T mutable (not readonly).

For example

interface Todo {
    readonly title: string;
    readonly description: string;
    readonly completed: boolean;
}
type MutableTodo = Mutable<Todo>; // { title: string; description: string; completed: boolean; }

答案

type Mutable<T extends object> = { -readonly [K in keyof T]: T[K] };

在线演示

标签:Typescript,string,title,readonly,boolean,体操,Mutable,description
From: https://www.cnblogs.com/laggage/p/type-challenge-mutable.html

相关文章

  • TypeScript 函数重载
    函数参数的类型可以使用联合类型?,让我们传递的值可以有多种类型的情况。下面是写的一个简单函数,参数x可以有number、string两种类型,返回值也是。functionreverse(x:......
  • Java: Immutable Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*不变模式ImmutablePatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-1......
  • Typescript类型体操 - PartialRequired
    题目中文实现一个通用的RequiredByKeys<T,K>,它接收两个类型参数T和K。K指定应设为必选的T的属性集。当没有提供K时,它就和普通的Required<T>一样使所有的属性成为必选的......
  • TypeScript(一)基本使用
    一:导入TypeScriptnpmitypescript或者npmitypescript-g(全局导入) 二:编译Ts文件为Js(道理跟Sass转Css一样)在当前文件目录终端中输入:tsc文件名称.ts然后就......
  • 03:TypeScript — 从初学者到专家 |对象、数组和函数
    03:TypeScript—从初学者到专家|对象、数组和函数级别:初学者我们已经了解了什么是变量以及如何使用语句设置它们。我们还看到了可用于指定值类型的不同原始类型。当我......
  • Typescript类型体操 - EndsWith
    题目中文实现EndsWith<T,U>,接收两个string类型参数,然后判断T是否以U结尾,根据结果返回true或false例如:typea=EndsWith<'abc','bc'>;//expectedtobefals......
  • Typescript类型体操 - StartsWith
    题目中文实现StartsWith<T,U>,接收两个string类型参数,然后判断T是否以U开头,根据结果返回true或false例如:typea=StartsWith<'abc','ac'>;//expectedtobe......
  • Typescript类型体操 - RemoveIndexSignature
    题目中文实现RemoveIndexSignature<T>,将索引字段从对象中排除掉.示例:typeFoo={[key:string]:any;foo():void;};typeA=RemoveIndexSignature<......
  • Typescript类型体操 - PickByType
    题目中文找出T中类型为U的属性示例:typeOnlyBoolean=PickByType<{name:string;count:number;isReadonly:boolean;isE......
  • Typescript类型体操 - MinusOne
    题目中文给定一个正整数作为类型的参数,要求返回的类型是该数字减1。例如:typeZero=MinusOne<1>;//0typeFiftyFour=MinusOne<55>;//54EnglishGivenanu......