亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在JavaScript文件中配置類型以獲取自動建議?

如何在JavaScript文件中配置類型以獲取自動建議?

喵喔喔 2022-09-16 21:11:00
在Javascript文件中獲取自動建議(帶有類型)應該配置什么?換句話說,給定一個用JS編寫的組件,如何獲得其道具的自動建議(這是可能的,請閱讀所有問題)。我正在嘗試獲取簡單按鈕組件的自動建議(以獲取“紅色”或“藍色”的建議),類似于MUI中的組件。// Would like the IDE so suggest "red" or "blue" on changing color propconst App = () => {  return <Button color="red" />;};// Button.jsimport React from "react";const Button = ({ color }) => <button style={{ color }}>Button</button>;export default Button;// ButtonTypes.d.tsexport interface Button {  color: "red" | "blue";}
查看完整描述

2 回答

?
HUH函數

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


查看完整回答
反對 回復 2022-09-16
?
守著星空守著你

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;


查看完整回答
反對 回復 2022-09-16
  • 2 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號