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;
添加回答
舉報