简体   繁体   English

d.ts文件导出模块

[英]d.ts file export module

I begin with Typescript and i try to create a new module but i have some problems. 我从Typescript开始,我尝试创建一个新模块,但我有一些问题。

I want to import a js file into my index.ts, so i know that I have to create a d.ts file in order to create and explain the function for typescript. 我想将一个js文件导入到我的index.ts中,所以我知道我必须创建一个d.ts文件才能创建和解释typescript的函数。 Into my index.ts i put this : 进入我的index.ts我把它:

import Toto from "./toto";

And this is my d.ts file : 这是我的d.ts文件:

declare module 'Toto' {

    function myfunction(callback : Function);

    export default myfunction;

}

But into my d.ts file i have this error : 但在我的d.ts文件中我有这个错误:

TS1128 : Declaration or Statement expected TS1128:预期声明或声明

I don't understand why because my test is very easy. 我不明白为什么,因为我的测试非常简单。

I use webpack to build the code, this is my webpack.config.js 我使用webpack来构建代码,这是我的webpack.config.js

module.exports = {
    entry: "./index.ts",
    output: {
        path: "bundle/",
        filename: "bundle.js"
    },
    resolve: {
        extensions : [".ts", ".tsx", ".js"]
    },
    module: {
        loaders: [
            { test: /\.tsx?$/, loader: "ts-loader" }
        ]
    }
};

And my tsconfig.json 还有我的tsconfig.json

{
  "compilerOptions": {
    "target" : "es5"
  }
}

If you have any ideas :) 如果你有任何想法:)

Regards 问候

If you want to load .js files directly just set the "allowJS" flag in the compiler options. 如果要直接加载.js文件,只需在编译器选项中设置“allowJS”标志即可。 TS will then try to use type inference based on the JS. 然后,TS将尝试使用基于JS的类型推断。 TS Compiler options TS编译器选项

So in you case just add the allowJS flag to the compiler options and skip the d.ts file. 因此,在您的情况下,只需将allowJS标志添加到编译器选项并跳过d.ts文件。

This set-up works for me 这个设置对我有用

//toto.js
function myfunction(callback) {
    callback("42")
}    
exports.default=myfunction


//toto.d.ts
export default function myfunction(callback : Function);


//main.ts
import myfunc from './toto'    
myfunc((res) => console.log(res) )


//tsconfig.json
{
    "compilerOptions": {
        "target" : "es5"
    }
 }

Running node main.js correctly logs 42 运行node main.js正确记录42


Alternatively using allowJs 或者使用allowJs

  • from the set-up above, remove the toto.d.ts definition file 从上面的设置中删除toto.d.ts定义文件
  • add "allowJs":true in tsconfig compilerOptions 在tsconfig compilerOptions中添加"allowJs":true

=> The first solution requires the crafting of a definition file, but you benefit from typings when using myfunc in main.ts . =>第一个解决方案需要制作定义文件,但是当你在main.ts使用myfunc时,你会受益于main.ts

Finally i found my problem, my EDI (phpStorm) was too old and it compile the code in Typescript 1.4 whereas i was in Typescript 2.2.1... 最后我发现了我的问题,我的EDI(phpStorm)太旧了,它在Typescript 1.4中编译代码,而我在Typescript 2.2.1 ...

Thanks for your feedback i don't know that i can use this option allowJs but for me it's not recommended because i want to document my code with typedoc and i need to create this d.ts file in order to do that. 感谢您的反馈,我不知道我可以使用此选项allowJs,但对我来说不推荐,因为我想用typedoc记录我的代码,我需要创建这个d.ts文件才能做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM