简体   繁体   English

如何键入命名的可选参数,该参数接受 object 以及在 TypeScript-React 中具有默认值的可选字段?

[英]How to type named, optional parameter that accepts an object with optional fields that have defaults in TypeScript-React?

I want something like the style prop in the default HTML components:我想要默认 HTML 组件中的style道具之类的东西:

 <div ref={ref} style={{ overflow: 'visible', height: 600, width: '100%' }}>

style itself is an optional prop, and the fields inside the object passed to style are optional too (and I assume they have defaults). style本身是一个可选的道具,传递给style的 object 中的字段也是可选的(我假设它们有默认值)。 I wish I could just look at the source code to figure out how it's done, but it just looks like complete gibberish to me.我希望我可以只看源代码来弄清楚它是如何完成的,但对我来说它看起来完全是胡言乱语。

Stuff I tried我试过的东西

function a({
  param1 = 'default param1',
  options = { option1: 'default option1', option2: 'default option2' },
}: {
  param1?: string;
  options?: { option1?: string; option2?: string };
}) {
  console.log(param1);
  console.log(options);
}

a({});
a({ param1: 'my param1' });
a({ param1: 'my param1', options: { option1: 'my option1' } });
[LOG]: "default param1" 
[LOG]: {
  "option1": "default option1",
  "option2": "default option2"
} 
[LOG]: "my param1" 
[LOG]: {
  "option1": "default option1",
  "option2": "default option2"
} 
[LOG]: "my param1" 
[LOG]: {
  "option1": "my option1"
} 

This almost works but the default for options is overridden completely when I only pass in option1 .这几乎可以工作,但是当我只传入option1时, options的默认值被完全覆盖。 How do I set defaults for each field in options individually?如何分别为options中的每个字段设置默认值?

You can do something along these lines if you only care about individual options:如果您只关心个别选项,则可以按照以下方式做一些事情:

function a({
  param1 = 'default param1',
  options,
}: {
  param1?: string;
  options?: { option1?: string; option2?: string };
}) {
  const {option1 = 'default option1', option2 = 'default option2'} = options ?? {}
  console.log(param1);
  console.log({option1, option2});
}

Alternatively, you could use Object.assign to build your options objects, eg或者,您可以使用Object.assign来构建您的选项对象,例如

const defaultOptions = {
   option1: 'default option1', 
   option2: 'default option2'
}
function a({
  param1 = 'default param1',
  options,
}: {
  param1?: string;
  options?: { option1?: string; option2?: string };
}) {
  const effectiveOptions = Object.assign({}, defaultOptions, options)
  console.log(param1);
  console.log(effectiveOptions);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 typescript-react 中为 ...rest 指定类型? - How can specify type for …rest in typescript-react? 如何在 typescript + React 中为 Object 类型的可选嵌套 props 提供默认值? - How do I provide default values to optional nested props of type Object in typescript + React? 使用React + TypeScript,当使用启用了strictNullChecks的默认值的可选道具时,如何避免类型转换? - With React + TypeScript, how do I avoid typecasting when using optional props with defaults with strictNullChecks enabled? 当属性是可选的时,如何在 Typescript 中解构对象? - How to descturcture an object in Typescript when properties are optional? 如何通过这个 TypeScript React 组件作为可选道具? - How to pass this TypeScript React component an optional prop? 如何使 typeof typescript 中的类型可选 - How to make type optional in typeof typescript 如何在 Typescirpt 中制作可选对象参数? - how to make optional object parameter in Typescirpt? 打字稿反应:根据另一个道具的类型有条件地选择道具 - Typescript React: Conditionally optional props based on the type of another prop 如何将可选参数传递给反应中的 function 和 javascript? - How to pass optional parameter to a function in react and javascript? Typescript - React,基于可选道具的条件回调道具类型 - Typescript - React, conditional callback prop type based on optional prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM