2 回答

TA贡献1836条经验 获得超4个赞
在此处查看更改。
// Button.d.ts
import * as React from "react";
declare const Button: React.FC<{
color: "red" | "blue";
}>;
export default Button;
// App.js
const App = () => {
return (
<>
{/* Auto-suggests "red" and "blue" */}
<Button color="red" />
</>
);
};
您需要使用与 相同的名称来命名该文件。此外,您需要声明,而不仅仅是道具。.d.ts.jsButton

TA贡献1799条经验 获得超8个赞
在JSDoc中使用类型脚本有另一种选择,它允许在javascript文件中出现语义错误:
// App.js
// @ts-check
const App = () => {
return (
<>
<Button color="red" />
{/* Shows a WARNING! */}
<Button color={null} />
</>
);
};
// types.ts
import type { CSSProperties, FunctionComponent } from "react";
export type ButtonComponent = FunctionComponent<{color: CSSProperties["color"]}>;
// Button.js
import React from "react";
/**
* @type {import("./types").ButtonComponent}
*/
const Button = ({ color }) => <button style={{ color }}>Button</button>;
export default Button;
添加回答
举报