首页 > 解决方案 > Typescript“声明命名空间”、“声明模块”在全局/模块范围内的区别

问题描述

我无法区分declare module "react"declare namespace React在全局/模块范围内的效果。

例如,我有两个文件:

// File A.ts
import * as React from "react";

export const Button: React.FC = () => {
  return <button data-test="whatever" />
}
/* File B.ts */
// 1:
declare module "react" {
  interface ButtonHTMLAttributes<T> {
    "data-test": string;
  }
}

// 2:
declare module "react" {
  interface ButtonHTMLAttributes<T> {
    "data-test": string;
  }
}
export {}

// 3:
declare namespace React {
  interface ButtonHTMLAttributes<T> {
    "data-test": string;
  }
}

//4:
declare namespace React {
  interface ButtonHTMLAttributes<T> {
    "data-test": string;
  }
}
export {}

这四个“声明”版本中的每一个都将对文件 A 中的“反应”符号做什么?版本 1 似乎完全覆盖了“反应”。版本 2 和 3 似乎改为“扩展”它。版本 3 似乎没有效果。

谢谢!

标签: typescriptmoduletypescript-typestypescript-namespace

解决方案


推荐阅读