1

While browsing a declaration file on dt I came accross the following syntax:

import github = Strategy; // <- what is imported from where here?

declare class Strategy extends oauth2.Strategy {
     ...
}

declare namespace Strategy {
    export import Strategy = github; // <- I've not seen this syntax before?

    interface _StrategyOptionsBase {
      ...
    }
}

export = Strategy;

I'm having trouble understanding the import and export syntax here. I roughly understand typescripts export= custom syntax. However, I don't understand why and what we're exporting from the namespace. Nor do I fully understand what we're importing with import github ....

I'd love some help.

1 Answer 1

2
import github = Strategy;

this syntax describes a namespace alias.

Another way that you can simplify working with namespaces is to use import q = x.y.z to create shorter names for commonly-used objects.

namespace Shapes {
  export namespace Polygons {
    export class Triangle {}
    export class Square {}
  }
}
import polygons = Shapes.Polygons;
let sq = new polygons.Square(); // Same as 'new Shapes.Polygons.Square()'

source


export import Strategy = github

is a combination of renaming the namespace and exporting it again with the new name. You can't write something like

declare namespace Strategy {
    export namespace Strategy;
}

so this looks like a workaround to achieve what is done in the passport-github source code.

// Load modules.
var Strategy = require('./strategy');

// Expose Strategy.
exports = module.exports = Strategy;

// Exports.
exports.Strategy = Strategy;

source

exports = module.exports = Strategy;

this line exports Strategy as default export, the counterpart in the typescript definition is

export = Strategy

Then passport-gitlab also export Strategy as property on the default export.

exports.Strategy = Strategy;

You can think of it like a class that has a static property which references the class.

class Strategy {
    static Strategy: typeof Strategy
}
export = Strategy
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.