webpack / schema-utils

Options Validation
MIT License
246 stars 39 forks source link

Typescript Support: Don't force consumers to enable esModuleInterop in tsconfig #110

Closed ozyman42 closed 4 years ago

ozyman42 commented 4 years ago

Because of this line it means that any project which consumes schema-utils either as a direct or transitive dependency must use the esModuleInterop tsconfig.json compilerOptions setting in order to compile their typescript.

Ideally we should not be forcing typescript users to add this setting if they don't want it. It also isnt necessary since in

Here's a demo:

  1. git clone https://github.com/AlexLeung/webpack-schema-utils-ajv-typescript-error-demo
  2. npm install
  3. npm start

You will see the following output, including a tsc error

> @ start /home/alex/GitProjects/experiments/webpack-schema-utils-ajv-typescript-error-demo                                                                                                                                                                                               
> tsc                                                                                                                                                                                                                                                                                     

node_modules/schema-utils/declarations/validate.d.ts:43:8 - error TS1259: Module '"/home/alex/GitProjects/experiments/webpack-schema-utils-ajv-typescript-error-demo/node_modules/ajv/lib/ajv"' can only be default-imported using the 'esModuleInterop' flag                             

43 import Ajv from 'ajv';                                                                                                                                                                                                                                                                 
          ~~~                                                                                                                                                                                                                                                                             

  node_modules/ajv/lib/ajv.d.ts:396:1                                                                                                                                                                                                                                                     
    396 export = ajv;                                                                                                                                                                                                                                                                     
        ~~~~~~~~~~~~~                                                                                                                                                                                                                                                                     
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.                                                                                                                                                      

Found 1 error.                                                                                                                                                                                                                                                                            

npm ERR! code ELIFECYCLE                                                                                                                                                                                                                                                                  
npm ERR! errno 2                                                                                                                                                                                                                                                                          
npm ERR! @ start: `tsc`                                                                                                                                                                                                                                                                   
npm ERR! Exit status 2                                                                                                                                                                                                                                                                    
npm ERR!                                                                                                                                                                                                                                                                                  
npm ERR! Failed at the @ start script.                                                                                                                                                                                                                                                    
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.                                                                                                                                                                                        

npm ERR! A complete log of this run can be found in:                                                                                                                                                                                                                                      
npm ERR!     /home/alex/.npm/_logs/2020-08-25T08_10_43_693Z-debug.log      
  1. To fix you can navigate to ./node_modules/schema-utils/declarations/validate.d.ts then change line 43 from
import Ajv from 'ajv';

to

import * as Ajv from 'ajv';
  1. Run npm start again and now the compilation succeeds.

The change described in step 4 is fine because we are only exporting types from the ajv package. I'll submit a PR for this shortly.

alexander-akait commented 4 years ago

Feel free to send a PR

ozyman42 commented 4 years ago

sent

alexander-akait commented 4 years ago

Fixed