Marcos Henrique da Silva
2 min readMay 11, 2020

--

Hi Felipe, thanks for the reply.

I got curious about your answer and I wen to search a bit and here’s what I found:
In theory, the express function is declared at the types with:

declare function e(): core.Express;

but at the same time it has the export at the end of the file

Also, at the same time the same file has

import * as qs from "qs";

and at the types definition and regarding your comment, they should be using require as well.

I am used to avoid using require when working with Typescript by practice and that was the first time that I got a bit concerned when should I change it or not, because even in the Typescript website its not completely clear if it is a bad practice or

https://www.typescriptlang.org/docs/handbook/modules.html#import

Then I got not so much clear answers here:

https://stackoverflow.com/questions/35706164/typescript-import-as-vs-import-require

and here

https://stackoverflow.com/questions/29596714/new-es6-syntax-for-importing-commonjs-amd-modules-i-e-import-foo-require/29598404#29598404

and here

https://2ality.com/2014/09/es6-modules-final.html

from this last one:

If you require a library in CommonJS, you get back an object:

var lib = require('lib');
lib.someFunc(); // property lookup

Thus, accessing a named export via lib.someFunc means you have to do a property lookup, which is slow, because it is dynamic.

In contrast, if you import a library in ES6, you statically know its contents and can optimize accesses:

import * as lib from 'lib';
lib.someFunc(); // statically resolved

===

That all said, I would be glad to read more about the correct pattern in a case that you have an official link to share.

Thanks again for reading and commenting the post

Best regards,

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response