Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend type define of NodeJS.Process.env #39

Closed
GongT opened this issue Sep 21, 2016 · 12 comments
Closed

extend type define of NodeJS.Process.env #39

GongT opened this issue Sep 21, 2016 · 12 comments

Comments

@GongT
Copy link

GongT commented Sep 21, 2016

installed: registry:dt/node#6.0.0+20160907103612

current content:

 export interface Process extends EventEmitter {
/// ....
    env: any;
/// ....
}

may change to:

export interface EnvironmentVariables {
    [key: string]: string;
}
export interface Process extends EventEmitter {
/// ....
    env: NodeJS.EnvironmentVariables;
/// ....
}

then users can extend env in their own .d.ts:

declare namespace NodeJS {
    export interface EnvironmentVariables {
        SOME_THING: string;
        SOME_OTHER_THING: string;
    }
}

currently this will cause error TS2403: Subsequent variable declarations must have the same type. Variable 'env' must be of type 'any', but here has type 'IProcessEnv':

interface IProcessEnv {
    SOME_THING: string;
    SOME_OTHER_THING: string;
}
declare namespace NodeJS {
    export interface Process {
        env: IProcessEnv;
    }
}
@blakeembrey
Copy link
Member

The dt definition comes from https://github.com/DefinitelyTyped/DefinitelyTyped. This should be possible if you install this definition (which is env~node).

@blakeembrey
Copy link
Member

blakeembrey commented Sep 21, 2016

Even then, I never got the code you posted to work. I tried different approaches, but nothing worked on the current TypeScript release unless you're using 2.0 or something.

Edit: Someone let me know if they have better luck and then we can change it from an inline type to an interface called Env.

@GongT
Copy link
Author

GongT commented Sep 23, 2016

I'm using typescript>2.0 ... LOL

tsc --version
Version 2.1.0-dev.20160905

@GongT GongT closed this as completed Sep 23, 2016
@voxlet
Copy link
Contributor

voxlet commented Oct 29, 2016

@blakeembrey

Edit: Someone let me know if they have better luck and then we can change it from an inline type to an interface called Env.

Confirmed this approach works with the current 2.0.6 TypeScript release.
This would be especially useful when using the TSLint no-string-literal rule, which currently needs liberal sprinkling of tslint:disable comment flags when accessing anything other than PATH.

@blakeembrey
Copy link
Member

I always disable that rule unfortunately, TypeScript development just kind of requires it because you can't access index signatures without it. It'd be nice if TypeScript allowed process.env.hello with an index signature, but it doesn't.

@voxlet
Copy link
Contributor

voxlet commented Nov 3, 2016

Yes, I guess that's true if you need to interface with a lot of external libraries. For my own code, though, I always define separate properties and use index signatures only for dynamic access.

Anyways, I went ahead and made a PR with the changes, if this is still up for consideration!

@xeoneux
Copy link

xeoneux commented Apr 27, 2017

@blakeembrey why do I not see these changes when I install @types/node? Are they not published yet?

@blakeembrey
Copy link
Member

This repo has nothing to do with NPM. Those come from DefinitelyTyped.

@xeoneux
Copy link

xeoneux commented Apr 27, 2017

@blakeembrey how do I install these types then?

@blakeembrey
Copy link
Member

This whole organisation is used for Typings, if you aren't using Typings, you can't. We've been waiting a year for microsoft/types-publisher#4 to make it possible.

@xeoneux
Copy link

xeoneux commented Apr 27, 2017

@blakeembrey is typings still in development or @types is the official solution?

@blakeembrey
Copy link
Member

Typings is stable and any issues will continue to be fixed, but it's in a deprecated state and I'd recommend using @types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants