Rastographics Rastographics - 1 year ago 144
TypeScript Question

How do you use Twix (momentjs) with Typescript

Has anyone used twix with typescript and could share how you got it working?

I have:

import * as moment from 'moment';
import * as twix from 'twix';

Moment is working fine, intellisense and everything.
Twix module imports fine and if I type twix. I can see the interface pop up.

But I get:

Property twix does not exist on Moment

When I try to use

I've also tried
let twix = require('twix');
import twix = require('twix');
with no success.

I ran
npm install moment
npm install twix
npm install @types/twix

I know moment is installed and works correctly.
The typings file being used for moment is the moment.d.ts file included right inside the moment npm package.

The typings file being used for twix is in @types/twix npm package.

After my imports, if I type
let t = twix.
then intellisense pops up with Twix, TwixFormatOptions, TwixIter, TwixParseAndFormatOptions, etc so I think typescript is recognizing the twix typings.

Answer Source

Try replacing:

import * as twix from 'twix';


let twix = require('twix');

From my understanding, Twix is written as a CommonJS module not an ES6 module hence the different import method.

a little more info on the difference here

EDIT: It appears there is something wrong with the 2.0 typing of twix. A workaround is to take a copy of the pre-2.0 twix typing from github and add it to your project.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download