简体   繁体   English

为什么与 Typescript 函数反应的解构道具需要传递一个对象?

[英]Why does destructuring props in react with Typescript function require passing an object?

I have recently started using react with typescript and it seems like something odd is happening when I destructure props with optional / default values.我最近开始使用 react with typescript ,当我使用可选/默认值解构 props 时,似乎发生了一些奇怪的事情。 For example take this function to generate a random id:例如用这个函数生成一个随机 id:

interface GenKeyIdProps {
  length?: number;
}

/**
 * Generate a random id (mainly for use with rendering lists of components.
 * genKeyId convert Math.random to base 36 (numbers + letters), and grab the first ${lenght} characters after the decimal.
 * @param length (optional) specify how many characters the id should be. default is 5.
 * @returns a string with number and letters of specified length.
 */
export const genKeyId = ({ length = 5 }: GenKeyIdProps) => Math.random().toString(36).substr(2, length);

The problem is when I try to call it:问题是当我尝试调用它时:

import { genKeyId } from '../../../helpers';

interface TextWrapperProps {
  textLines: string[];
}

const TextWrapper = ({ textLines }: TextWrapperProps) => {

  return textLines.map(line => (<div key={genKeyId()}>{line}</div>));

};

export default TextWrapper;

if I do not pass at least an empty object to genKeyId I get the following error: Expected 1 arguments, but got 0.ts(2554).如果我没有将至少一个空对象传递给 genKeyId,我会收到以下错误:预期为 1 个参数,但得到 0.ts(2554)。

Why do I need to pass an empty object?为什么我需要传递一个空对象? Is my GenKeyIdProps interface specifying the props need to be part of an object?我的 GenKeyIdProps 接口是否指定道具需要成为对象的一部分? I am a bit confused about this.我对此有些困惑。

Is my GenKeyIdProps interface specifying the props need to be part of an object?我的 GenKeyIdProps 接口是否指定道具需要成为对象的一部分?

Yes, you've defined GenKeyIdProps as an object.是的,您已经将GenKeyIdProps定义为一个对象。 All interface definitions are objects.所有interface定义都是对象。 Your GenKeyIdProps is roughly equivalent to:您的GenKeyIdProps大致相当于:

type GenKeyIdProps = {
  length?: number;
};

That is, an object with an optional length property of type number .也就是说,具有number类型的可选length属性的对象。

There's no need for genKeyId to accept an object, though;但是, genKeyId不需要接受对象; it's not a component function, it's just a utility function.它不是一个组件函数,它只是一个效用函数。 You could define it to accept length directly by removing the destructuring:您可以通过删除解构将其定义为直接接受length

export const genKeyId = (length = 5) => Math.random().toString(36).substr(2, length);

Alternatively, if you do want it to accept an object but you want it to be able to be called without one, you can default the parameter you're destructuring:或者,如果您确实希望它接受一个对象,但希望它能够在没有对象的情况下被调用,则可以默认您正在解构的参数:

export const genKeyId = ({ length = 5 }: GenKeyIdProps = {}) => Math.random().toString(36).substr(2, length);
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^

Now you can call genKeyId A) with an object that has a length ;现在您可以使用length为 的对象调用genKeyId A) ; B) with an object that doesn't have a length ; B) 对象没有length and C) with no argument at all.和 C) 根本没有争论。

It's up to you whether you want getKeyId to accept a number or an object with a length property.您是否希望getKeyId接受数字或具有length属性的对象取决于您。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM