TypeScript module augmentation overwrites the original module? -
in node.js / express app, i've had headers.ts file quite while content:
type httpheader = 'x-my-header' | 'x-my-other-header' | 'x-another'; declare module 'express-serve-static-core' { import * http 'http'; interface request extends http.incomingmessage, express.request { header(name: httpheader): string | undefined; } }
it used compile fine, however, after recent rm -rf node_modules
, npm install
again, i'm getting lot of errors
error ts2339: property 'get' not exist on type 'request'. error ts2339: property 'end' not exist on type 'response'.
it seems core issue node_modules/@types/express/index.d.ts
resolves import * core "express-serve-static-core"
small augmentation , skips loading of real thing entirely. don't know why because have folder node_modules/@types/express-serve-static-core
installed.
what be?
judging the:
import * http 'http';
inside of module declaration, not, in fact writing module "agumentation" want, rather replacing existing module.
in order write module augmentation need write this:
import { request} 'express-serve-static-core'; import * http 'http'; export type httpheader = 'x-my-header' | 'x-my-other-header' | 'x-another'; declare module 'express-serve-static-core'{ export interface request extends http.incomingmessage, express.request { header(name: httpheader): string | undefined; } }
the first thing note, should me external "file" module (it should have imports , exports).
the second thing note import * http
should go outside module augmentation @ not legal inside.
the declared module serves strictly augmentation. not over-write or replace existing express-server-static-core
module. in fact, module required exist augmented (if misspell module name not compile' example).
i cannot tell example why code worked before. perhaps there differences in how express-server-static-core
declaration files implemented before. if follow example, things should work you.
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from TypeScript Online Training . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai
ReplyDelete