为此模块添加类型定义的正确方法是什么
what is the correct way to add type definition for this module
我有一个没有任何打字稿定义的模块,我在我的代码中这样使用它:
import * as Switch from 'react-bootstrap-switch';
render () {
return <Switch/>
}
由于 react-bootstrap-switch
没有类型定义,我正在尝试为其创建一个。
我试过:
declare module 'react-bootstrap-switch'
{
import React = require("react");
class Switch extends React.Component<{onColor?:string},{} >
{}
export = Switch;
}
然而,这会导致原始代码中的导入语句抛出此错误:
error TS2497: Module ''react-bootstrap-switch'' resolves to a non-module entity and cannot be imported using this construct.
满足上面代码的模块定义的正确写法是什么?
尝试移除声明模块部分并让您的 Switch class 声明如下:
import React = require("react");
export default class Switch extends React.Component<{onColor?:string},{} >
{}
然后像这样导入:
import Switch from 'react-bootstrap-switch';
或者如果您愿意:
import React = require("react");
export class Switch extends React.Component<{onColor?:string},{} >
{}
和
import {Switch} from 'react-bootstrap-switch';
编辑
如果您只是创建定义来修复您的错误,您应该使用如下语法导入您的定义:
import Switch = require('react-bootstrap-switch');
希望对您有所帮助。
import
语句用于 .ts
文件而不是 .d.ts
文件。对于那些您需要使用 ///<reference .../>
标签或简单的 import React = __React;
类型导入。
这是我可以使用 react.d.ts file and the 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 版本仍然按预期加载并且仍然 运行s.
我需要感谢 this post and 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"
]
}
main.tsx
/// <reference path="./typings/react/react.d.ts" />
/// <reference path="./switch.d.ts" />
import React = __React;
import * as Switch from 'react-bootstrap-switch';
var HelloMessage = React.createClass({
render: function() {
return <Switch.Component name="test" />
}
});
switch.d.ts
///<reference path="./typings/react/react.d.ts"/>
declare module 'react-bootstrap-switch'
{
import React = require("react");
export class Component extends React.Component<any, any> {}
}
尝试将以下 3 行添加到您的原始定义中:
declare module 'react-bootstrap-switch'
{
import React = require("react");
class Switch extends React.Component<{onColor?:string},{}>
{}
namespace Switch {}
var s: Switch;
export = Switch;
}
您的 import * as Switch from 'react-bootstrap-switch';
应该不需要更改。
这已被确定为 TypeScript 中的错误:
https://github.com/Microsoft/TypeScript/issues/6656
我有一个没有任何打字稿定义的模块,我在我的代码中这样使用它:
import * as Switch from 'react-bootstrap-switch';
render () {
return <Switch/>
}
由于 react-bootstrap-switch
没有类型定义,我正在尝试为其创建一个。
我试过:
declare module 'react-bootstrap-switch'
{
import React = require("react");
class Switch extends React.Component<{onColor?:string},{} >
{}
export = Switch;
}
然而,这会导致原始代码中的导入语句抛出此错误:
error TS2497: Module ''react-bootstrap-switch'' resolves to a non-module entity and cannot be imported using this construct.
满足上面代码的模块定义的正确写法是什么?
尝试移除声明模块部分并让您的 Switch class 声明如下:
import React = require("react");
export default class Switch extends React.Component<{onColor?:string},{} >
{}
然后像这样导入:
import Switch from 'react-bootstrap-switch';
或者如果您愿意:
import React = require("react");
export class Switch extends React.Component<{onColor?:string},{} >
{}
和
import {Switch} from 'react-bootstrap-switch';
编辑
如果您只是创建定义来修复您的错误,您应该使用如下语法导入您的定义:
import Switch = require('react-bootstrap-switch');
希望对您有所帮助。
import
语句用于 .ts
文件而不是 .d.ts
文件。对于那些您需要使用 ///<reference .../>
标签或简单的 import React = __React;
类型导入。
这是我可以使用 react.d.ts file and the 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 版本仍然按预期加载并且仍然 运行s.
我需要感谢 this post and 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"
]
}
main.tsx
/// <reference path="./typings/react/react.d.ts" />
/// <reference path="./switch.d.ts" />
import React = __React;
import * as Switch from 'react-bootstrap-switch';
var HelloMessage = React.createClass({
render: function() {
return <Switch.Component name="test" />
}
});
switch.d.ts
///<reference path="./typings/react/react.d.ts"/>
declare module 'react-bootstrap-switch'
{
import React = require("react");
export class Component extends React.Component<any, any> {}
}
尝试将以下 3 行添加到您的原始定义中:
declare module 'react-bootstrap-switch'
{
import React = require("react");
class Switch extends React.Component<{onColor?:string},{}>
{}
namespace Switch {}
var s: Switch;
export = Switch;
}
您的 import * as Switch from 'react-bootstrap-switch';
应该不需要更改。
这已被确定为 TypeScript 中的错误: https://github.com/Microsoft/TypeScript/issues/6656