Closed
Description
Hi,
I am trying to write the type definition file(.d.ts
) for bunyan-middleware but encounter two problems that I couldn't figure out how to resolve.
A little bit of context: bunyan-middleware is an express middleware that attaches a bunyan logger object to each express incoming req
object as req.<propertyName>
, where <propertyName>
is controlled by the bunyan-middleware option.propertyName
.
Now the .d.ts
file I tried:
/// <reference path="../bunyan/bunyan.d.ts" />
/// <reference path="../express/express.d.ts" />
declare module Express {
// Problem1: Error get from tsc 1.6.2
// error TS1147: Import declarations in a namespace cannot reference a module.
import * as bunyan from 'bunyan';
export interface Request {
// Problem2: Have not found a way to dynamic create the property(e.g. propertyName)
// NOTE: 'log' is the default property name when propertyName option is omitted
log: bunyan.Logger;
}
}
declare module "bunyan-middleware" {
import * as bunyan from 'bunyan';
import * as express from 'express';
module e {
interface Options {
logger: bunyan.Logger;
propertyName?: string;
}
}
function e(options: e.Options): express.RequestHandler;
export = e;
}
So two problems(also inline in the source above):
- How do I type the
log
property asbunyan.Logger
? How can I reference an ambient external module while doing declaration merging forexpress
? - How can I correctly add the property name to
express.Request
based on an input option(option.propertyName
)?
Thanks