Skip to content

Dynamic property name in interface definition and ambient external module problem #5644

Closed
@ericlu88

Description

@ericlu88

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):

  1. How do I type the log property as bunyan.Logger? How can I reference an ambient external module while doing declaration merging for express?
  2. How can I correctly add the property name to express.Request based on an input option(option.propertyName)?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions