3

I have a file called 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?

1 Answer 1

2

if you have access to file, just change the extension to .ts and then simply write below style import

import { getTypes , someotherExport } from './yourfile' //above statement import individual exports or you can import everything in a variable like

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

if you don't have access to it, then you can try require

declare T:any;
let T = require('yourJS');
T.getTypes()
Sign up to request clarification or add additional context in comments.

4 Comments

Note that this is only true in TS 2.1
thank you it mostly works, except it can not find lodash.
it still is looking for localhost:3000/lodash.js and not finding it. Not clear how to load lodash in a ts file. I have tried npm install --save @types/lodash and import * from "lodash"... still the same error
lodash has typings so you can do, typings install dt~lodash. and use as import * as _ from lodash or import { functionName } from lodash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.