打工很辛苦吧 2023-07-24 12:20 采纳率: 26.3%
浏览 23

如何通过不import的方式使用类型

使用react+vite+ts创建的项目,我在项目根目录下创建了一个types文件夹,里面有一个global.d.ts

import { ReactNode } from "react"

declare interface PublicProps {
    children?: ReactNode
}

我现在有个ui组件Pull.tsx。我想在不通过import的方式能直接使用我这个PublicProps

//Cannot find name 'PublicProps'
const Pull = (props:PublicProps) => {
    return <div></div>
} 

我的tsconfg.json如下

{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",//"bundler",
    "allowImportingTsExtensions": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
    }
  },
  "include": [
    "src",
    "types/**/*.d.ts",
  ],
  "references": [{ "path": "./tsconfig.node.json" }]
}
  • 写回答

1条回答 默认 最新

  • hahai1212 2023-07-24 13:48
    关注

    在 tsconfig.json 中配置 TypeScript, include 属性包含了需要校验 ts 的文件。ts 默认会将 xx.d.ts 类型文件中的类型注册成全局的。
    示例: "include": ["src//*", "types//.d.ts", "mock/**/.ts", "src/pages/part-admin"],

    评论

报告相同问题?

问题事件

  • 修改了问题 7月26日
  • 创建了问题 7月24日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大