首页 > 其他分享 >[Typescript] Excess Properties in Functions

[Typescript] Excess Properties in Functions

时间:2024-08-01 14:50:57浏览次数:10  
标签:index Functions Typescript const name Properties User id users

interface User {
  id: number;
  name: string;
}

const users = [
  {
    name: 'Waqas',
  },
  {
    name: 'Zain',
  },
];

const usersWithIds: User[] = users.map((user, index) => ({
  ...user,
  id: index,
  // @ts-expect-error
  age: 30,
}));

The above code doesn't emit any compiler error from Typescript, but the ageis not expected in User type, we need to figure out a way to let Typescript to report error for it.

 

Solution:

interface User {
  id: number;
  name: string;
}

const users = [
  {
    name: 'Waqas',
  },
  {
    name: 'Zain',
  },
];

function transformUser(user: { name: string }, index: number): User {
  return {
    ...user,
    id: index,
    // @ts-expect-error
    age: 30,
  };
}

const usersWithIds = users.map(transformUser);

 

Another way:

interface User {
  id: number;
  name: string;
}

const users = [
  {
    name: 'Waqas',
  },
  {
    name: 'Zain',
  },
];

const usersWithIds = users.map((user, index) => ({
  ...user,
  id: index,
  // @ts-expect-error
  age: 30
} satisfies User));

 

标签:index,Functions,Typescript,const,name,Properties,User,id,users
From: https://www.cnblogs.com/Answer1215/p/18336657

相关文章

  • Python - Creating jump tables using lambda functions
    Wecanplacelambdafunctioninsidelistanddictionaryliterals.Thiswaywecanuselambdaexpressionstocreatejumptables.>>>L=[lambdas:s.strip().lower(),... lambdas:s.strip().upper(),... lambdas:s.lstrip().title(),... lambd......
  • Python - Creating Managed Attributes using properties
    CreatingManagedAttributesusingpropertiesPropertiescanbeusedtocreatedataattributeswithspecialfunctionality.Ifyouwantsomeextrafunctionality(liketypechecking,datavalidationortransformation)whilegettingorsettingadataattribut......
  • typescript基础语法补充
    TypeScript是一种由微软开发的开源编程语言,它是JavaScript的一个超集,增加了静态类型和一些面向对象的特性。下面是对TypeScript的一些关键知识点的总结:TypeScript的特点超集:TypeScript完全兼容JavaScript,所有有效的JavaScript代码也是有效的TypeScript代码。静态类型:Ty......
  • [Typescript] handle event.target type in Form
    TheerrorweencounteredinthischallengewasthattheEventTarget|nulltypewasincompatiblewiththerequiredparameteroftypeHTMLFormElement.Theproblemstemsfromthefactthatthesetypesdon'tmatch,andnullisnotpermitted:constdata......
  • Python - Using a list with functions from the random module
    Toselectarandomitemfromthelistorshufflethelist,youcanusethechoiceandshufflefunctionsfromtherandommoduleofthestandardlibrary.Therandom.choice()functionreturnsarandomlyselectedelementfromthelist.>>>importran......
  • [Typescript] Restrict available operations on values using value objects
    ValueObjectsareanotherpatterninDomain-drivenDesignthatprovidemorestructurearoundwhatyoucanandcannotdowithatype.InTypeScriptwecreateValueObjectswithclassesthatwilldefinewhatoperationscanbeperformedtothevalueonthec......
  • InputStream inputStream = classLoader.getResourceAsStream("aaa.properties") ; 
    问:InputStreaminputStream=classLoader.getResourceAsStream("aaa.properties"); 获取到的 inputStream 是null答:当您尝试使用ClassLoader的getResourceAsStream方法来获取一个资源文件(如"aaa.properties")的InputStream,但得到的结果是null时,这通常意味着资源文......
  • typescript: vscode create project
       npmcreatevue@latestcdvue-projectnpmi-Dtypescriptnpminstall-gtypescriptts-nodenpminstallwebpack-gnpminstall-g@vue/clinpminstall-gtypescripttsc--versionnpminstall--gcreate-vueornpminstall--g@vue/clinp......
  • SpringBoot 配置文件详解:properties 和 yml
    目录一、配置文件的作用二、配置文件的格式三、properties配置文件说明 3.1 properties基本语法3.2读取配置文件四、yml配置文件说明4.1yml基本语法4.2yml读取文件4.3yml使用进阶4.3.1配置对象4.3.2配置集合4.3.3配置Map一、配置文件的作用配置文......
  • Antd报错Cannot read properties of undefined (reading 'createElement')
    1、代码root=createRoot(document.getElementById("materialCertification"));root.render(<DisplayUI/>);2、报错信息logger.js:205Cannotreadpropertiesofundefined(reading'createElement')[email protected]:205_processError......