javascript - importing goes wrong with systemjs -


i'm trying use existing js library (validate.js) in both client , server.

i installed using npm, , compiles both server , client.
when using in server works great, when execute in browser throws error.

the same file used in both cases:

import validate = require("validate.js");  export function requestvalidator(data: any): {     return (validate any)(data, constraints, { allowempty: true }); } 

validate asserted any becuase otherwise get:

ts2349: cannot invoke expression type lacks call signature.

the .d.ts i'm using is:

declare module "validate.js" {     export interface validatejs {         (attributes: any, constraints: any, options?: any): any;         async(attributes: any, constraints: any, options?: any): promise<any>;         single(value: any, constraints: any, options?: any): any;     }      export const validate: validatejs;     export default validate; } 

the module exports function, , works in server, in client when invoking function get:

uncaught typeerror: validate not function(…) 

the code compiled using target commonjs server:

"use strict"; const validate = require("validate.js"); ... 

and system client:

system.register(["validate.js"], function(exports_1, context_1) {     "use strict";     var __modulename = context_1 && context_1.id;     var validate;     ...      return {         setters:[             function (validate_1) {                 validate = validate_1;             }],     ... 

when debugging it, validate indeed isn't function it's:

validate: r     empty_string_regexp: (...)     empty_string_regexp: function()     set empty_string_regexp: function()     promise: (...)     promise: function()     set promise: function()     __usedefault: (...)     __usedefault: function()     set __usedefault: function()     async: (...)     async: function()     set async: function()     capitalize: (...)     capitalize: function()     set capitalize: function()     cleanattributes: (...)     cleanattributes: function()     set cleanattributes: function()     ... 

any idea what's going on , why behaves way in browser?

when compile "module": "system", node-compatible import

import validate = require("validate.js"); 

no longer works - value getting validate module, not function. may bug in typescript, or design - don't know. (update: github comment addressed jsonfreeman here, looks it's design: you module object set of properties including 1 named default).

there several ways around this.

first, can easy conversion - function need provided default property of module, line fix it:

validate = validate.default ? validate.default : validate; 

or, can compile "module": "commonjs" browser, typescript generate working code, , systemjs detect format modules automatically.

or finally, still compile "module": "system", import validate.js it's intended in typings:

import validate 'validate.js'; 

that way don't have casts any, , typescript generate necessary access default property, drawback not work @ in node when imported way.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -