Require in Typescript

Can someone explain how this looks in TypeScript. Can this be done through imports?

JS:

var casper = require('casper').create({}) 

CoffeeScript:

 casper = require("casper").create() 

Visual Studio Error : Error 1 The name "casper" does not exist in the current scope

+4
source share
6 answers

I put together a blog using require.js with Typescript.
http://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/ You can write this code:

 require["casper", (casper) => { var item = casper.create(); }; 
+1
source
 import something = module('something'); var somevar = something.create({}); 

It should be noted here that TS does not allow truncating a module (β€œsomething”) today, so you need to split your statement into two.

+7
source

If you use the module flag when compiling TypeScript:

 --module commonjs 

or

 --module amd 

It converts the import into the appropriate module load statement:

 import something = module('something'); var somevar = something.create(); 
+1
source

If this thing is written for some reason in JavaScript, you also need to declare a module for it (otherwise it will not compile).

By convention, you place such an ad in a separate file called casper.d.ts:

 declare module casper { export class SomeClassInCasper { } export function someFunctionInCasper(); } 

And you should include a link to this casper.d.ts at the top of your import / module:

 /// <reference path="casper.d.js" /> import casper = import("casper"); 
0
source

casper is installed through npm in the global area.

  • typings install dt ~ casperjs --global --save
  • declare var casper; (according to your code)

A short case with typescript as you can see here:

https://medium.com/@guyavraham/smoke-tests-casper-js-with-typescript-8b01b504f3a4

0
source

I wrote a fairly detailed post about using Require in TypeScript. My approach does NOT require you to use ugly

 require(['dependency'], function() { } 

syntax. Instead, in TypeScript, you can use the convenient syntax import x = module("x") .

-1
source

Source: https://habr.com/ru/post/1446069/


All Articles