简体   繁体   English

在JavaScript中导入/请求文件(而非模块)时如何使用类型定义?

[英]How to use type definitions when importing/requiring files (not modules) in JavaScript?

I have three files: 我有三个文件:

a.js a.js

var moduleb=require('./b.js')
moduleb.func('should_be_a_string_and_return_a_boolen')

b.js b.js

 function func(arg){
         return arg
 }
 module.exports={func}

bdts bdts

 declare function fnc(arg:string):boolean

The question: 问题:

How I make TypeScript compiler to understands the type definition in bdts and connect it to the exported values in b.js ? 如何使打字稿编译器理解类型定义bdts并将其连接到出口值b.js

What I'm trying to achieve? 我正在努力实现什么?

I'm using create-react-app and I want to add type definition to Components so that I can get IntelliSense, to all the props of the components. 我正在使用create-react-app并且想向组件添加类型定义,以便可以将IntelliSense应用于组件的所有道具。

Thanks! 谢谢!

Modules (both CommonJS and ES) that have respective .d.ts typings can be imported as usual in TypeScript: 可以分别在TypeScript中导入具有各自.d.ts类型的模块(CommonJS和ES):

import * as moduleb from './b.js';

Or: 要么:

import moduleb = require('./b.js');

While require without import statement is supposed to be used for modules that don't have typings and synchronous dynamic imports. 虽然require no import语句应该用于没有类型和同步动态导入的模块。

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

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