import 语句用于.ts 文件而不是.d.ts 文件。对于那些你需要使用///<reference .../> 标签或简单的import React = __React; 类型导入的人。
这是我使用react.d.ts 文件和React Switch 源代码所能想到的。
///<reference path="../react/react.d.ts"/>
declare module 'react-bootstrap-switch'
{
function Switch(): __React.ClassicComponentClass;
export = Switch;
}
如何使用 typescript 和如何为 JS 库编写定义文件是两件完全不同的事情。如果这对您不起作用,那么我想我可以删除存储库并构建它。
更新
好的,我有一个工作版本
declare module 'react-bootstrap-switch'{
interface SwitchProps{
onColor?: string
}
class Switch extends __React.Component<SwitchProps, {}>{}
export = Switch;
}
使用import Switch = require('react-bootstrap-switch');
在你评论说你不能切换导入之前......
请阅读答案here 解释原因。请运行代码。您会看到 JS 版本的库仍按预期加载并仍在运行。
我需要感谢 this post 和 this definition 在这方面的帮助。
我的编译测试文件:
import * as React from 'react';
import Switch = require('react-bootstrap-switch');
var b = <div></div>;
var a = <Switch/>;
结果输出:
var React = require('react');
var Switch = require('react-bootstrap-switch');
var b = React.createElement("div", null);
var a = React.createElement(Switch, null);
如您所见,任一导入的输出都是相同的,并且构建没有错误。
还有 tsconfig.js:
{
"compilerOptions": {
"module": "commonjs",
"target": "es3",
"noImplicitAny": false,
"outDir": "built",
"rootDir": ".",
"sourceMap": false,
"moduleResolution": "node",
"jsx": "react"
},
"exclude": [
"node_modules"
]
}