Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module can't be used in typescript #988

Closed
radio-miskovice opened this issue Apr 5, 2019 · 6 comments
Closed

Module can't be used in typescript #988

radio-miskovice opened this issue Apr 5, 2019 · 6 comments
Labels

Comments

@radio-miskovice
Copy link

I am missing examples how to use this module in typescript. The usual typescript patterns do not work. ajv.d.ts does not make much sense, it cannot be used to import anything useful (executable functions or objects) into a typescript program.
Could you please show some working example?

@epoberezkin
Copy link
Member

@radio-miskovice please see the usage in any dependant library / codebase or please submit the question to StackOverflow

@jscharett
Copy link

Having a bit of a problem here as well. I'm able to import is like so import * as Ajv from 'ajv' and everything seems to resolve, however when I go to compile, the compiler throws error "Cannot use 'new' with an expression whose type lacks a call or construct signature" when trying to instantiate a new AJV instance.

@jscharett
Copy link

Looks like the new --esModuleInterop flag in Typescript compiler prevents the import of AJV. Previously, you able to simply import like so import * as Ajv from 'ajv' but, with Typescript 2.7+, when turning on esModuleInterop, you get an error. This is intended per the docs. Looks like the type definition needs to be updated?

@jscharett
Copy link

So I was finally able to nail down the syntax I needed. Using import Ajv from 'ajv'; works to import the namespace and class, however import * as Ajv from 'ajv'; and import { Ajv } from 'ajv'; do not. Sorry for the confusion.

@adrianolek
Copy link

Are the type definitions correct, though?
When importing import Ajv from "ajv"; and trying to use Ajv as a type you'll get Cannot use namespace 'Ajv' as a type. from tsc.
You could import { Ajv } from "ajv"; - that would fix the tsc issue, but the module doesn't actually export it, so in runtime it won't work.
Am I missing something?

@sepbot
Copy link

sepbot commented Jan 12, 2020

Looks like you can do

import Ajv from 'ajv';

with :

// tsconfig.json
"allowSyntheticDefaultImports": true,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants