简体   繁体   English

Typescript JSDoc …Rest 类型语法

[英]Typescript JSDoc …Rest type syntax

Got a problem when beginning with typescript on an existing large react/redux application.在现有的大型 react/redux 应用程序上从 typescript 开始时遇到问题。

As a proof of concept, I've converted one of my react files to a.ts file.作为概念证明,我已将我的一个反应文件转换为 a.ts 文件。 I'm trying to add types using JSDoc to the imported JS files to tell Typescript what params are available (instead of just declaring the module as any in adts file).我正在尝试将使用 JSDoc 的类型添加到导入的 JS 文件中,以告诉 Typescript 哪些参数可用(而不是仅在 adts 文件中将模块声明为任何参数)。

My issue is with a "rest" parameter that's used in a react functional component to pass props through to a another react component.我的问题是在反应功能组件中使用的“休息”参数将道具传递给另一个反应组件。 In the below example, Typescript is identifying the prop "id" as not existing.在下面的示例中,Typescript 将道具“id”标识为不存在。

.tsx file: .tsx 文件:

import ReactFunction from 'wherever_it_is/react_function';

//inside another react component
<ReactFunction
    prop1="something"
    id="unique"
/>

wherever_it_is/react_function.jsx file: where_it_is/react_function.jsx 文件:

/**
 * @typedef {object} ReactFunctionProps
 * @prop {boolean} prop1 - descriptive text
 * @prop {...any} ...otherProps - pass through props
 */

/**
 * descriptive comment about ReactFunction
 *
 * @param {ReactFunctionProps} props
 * @return {import("@types/react").JSX.Element}
 */
export default function ReactFunction({
    prop1,
    ...otherProps
}) {
    return (
        <OtherThing
            {...otherProps}
        />
    );
}

Using Typescript 4.1.3.使用 Typescript 4.1.3。

Anyone know the correct syntax to Typescript JSDoc a "...rest" operator?任何人都知道 Typescript JSDoc 一个“...rest”运算符的正确语法吗? As far as I can tell, I am using the correct syntax from https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html where the rest operator is mentioned.据我所知,我使用的是https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html中提到的 Z65E8800B5C6800AAD896F88 运算符的正确语法。

Any other suggestions?还有其他建议吗? (other than declare module wherever_it_is/react_function; which I'm aware will import the module as any -- trying not to resort to that yet) (除了declare module wherever_it_is/react_function;我知道它会像any一样导入模块——尽量不要诉诸于此)

A workaround is to create adts file and define the type in Typescript syntax.一种解决方法是创建 adts 文件并在 Typescript 语法中定义类型。

wherever_it_is/react_function.d.ts: where_it_is/react_function.d.ts:

export default function ReactFunction({
    prop1,
    ...otherProps 
}: {
    [x: string]: any;
    prop1: any;
}): JSX.Element;

While this technically doesn't answer the question, it is a way forward.虽然这在技术上并不能回答这个问题,但它是一种前进的方式。 My suspicion is that the...rest use case isn't supported by the JSDoc typing method.我的怀疑是 JSDoc 键入方法不支持 ...rest 用例。

I found the @typedef way, where you constructs new Props type and then just intersect it with React.HTMLAttributes like in pure Typescript syntax:我找到了@typedef 方式,您可以在其中构造新的 Props 类型,然后将其与 React.HTMLAttributes 相交,就像纯 Typescript 语法一样:

/**
 * @typedef {Object} TextInputProps
 * @property {string} [className]
 * @property {string} label
 */

/**
 * @param {TextInputProps & React.InputHTMLAttributes<HTMLInputElement>} props
 */
function TextInput({
    className = '',
    label = '',
    ...props
}) {...}

That way seems a little weird, because you need to mix Typescript types and functionality with JSdoc syntax, but it worked by me.这种方式似乎有点奇怪,因为您需要将 Typescript 类型和功能与 JSdoc 语法混合,但它对我有用。

You can also see square brackets for classname param - you can mark that param as optional.您还可以看到classname参数的方括号 - 您可以将该参数标记为可选。

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

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