简体   繁体   English

如何在打字稿中使用 javascript 模块

[英]how to use a javascript module in typescript

I have a file called model-parser.js我有一个名为 model-parser.js 的文件

it contains:它包含了:

var _ = require('lodash');

function getTypes(vitalsModel) {
    var vitals = _.get(vitalsModel, 'vitals');

    var rows = [];
    _.forEach(vitals, function (vital) {
        rows.unshift(Object.keys(vital));
    });

    return rows; 
} 

module.exports.getTypes = getTypes;

I want to use the above file in a typescript file.我想在打字稿文件中使用上述文件。 What is the correct syntax?什么是正确的语法?

if you have access to file, just change the extension to .ts and then simply write below style import如果您有权访问文件,只需将扩展名更改为 .ts,然后只需在样式导入下方写入

import { getTypes , someotherExport } from './yourfile' //above statement import individual exports or you can import everything in a variable like import { getTypes , someotherExport } from './yourfile' //上面的语句导入单个导出,或者您可以导入变量中的所有内容,例如

import * as T from './yourfile'
//use it
T.getTypes()

if you don't have access to it, then you can try require如果您无权访问它,那么您可以尝试 require

declare T:any;
let T = require('yourJS');
T.getTypes()

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

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