Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

[Lodash] static var not working anymore #367

Open
farfromrefug opened this issue Aug 5, 2017 · 12 comments
Open

[Lodash] static var not working anymore #367

farfromrefug opened this issue Aug 5, 2017 · 12 comments

Comments

@farfromrefug
Copy link

With the latest typings for lodash i can't use the static declare of _

My project do not "import" lodash anywhere but it is available still.
For me the typings were working before thanks to:

declare var _: _.LoDashStatic;

But now there is some export so the declare is only in the scope of the module.
Is that on purpose?

An easy way to fix it if not on purpose is to replace:

declare var _: _.LoDashStatic;

with

declare global {
    var _: _.LoDashStatic;
}

@ghost
Copy link

ghost commented Aug 8, 2017

export as namespace _; should be declaring the global variable you need. What's the precise error you're getting?

@farfromrefug
Copy link
Author

@andy-ms no it won't declare it as global outside of the module. At least not for me.
I think it is because of export = _; which makes it a module declaration. And by such to augmente the global scope you need to use declare global

But it might be the right way to go by not declaring _ in global and instead expect import * as _ from 'lodash'
I need my case i found a solution by creating a lodash.d.ts in my project with this

import * as _ from 'lodash'

declare global{
    var _:_.LoDashStatic & {
        remove<T, K extends keyof T>(
            object: T,
            key?: K,
            defaultValue?: T[K]
        ): T[K]
        getKeyByValue(object, value)
        mapIfDefined(array, func)
        mod(n, m)
        move(array, oldIndex, newIndex)
        pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>
    }
}

As you can see it also shows how to extend lodash definitions.

I won't close it until you decide if it's a normal behavior now for lodash not to extend global scope.

@ghost
Copy link

ghost commented Aug 9, 2017

Can you provide an example project that reproduces the error?

@farfromrefug
Copy link
Author

@andy-ms I think a default project with lodash package installed but not imported would suffise.

@ghost
Copy link

ghost commented Aug 9, 2017

No, that works just fine for me.

@farfromrefug
Copy link
Author

farfromrefug commented Aug 10, 2017

can you try with that tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "moduleResolution": "node",
        "module": "commonjs",
        "preserveConstEnums": true,
        "declaration": true,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noImplicitUseStrict": true
    },
    "include": [
          "Resources/**/*.ts"
      ]
}

Just change the "include"

@ghost
Copy link

ghost commented Aug 10, 2017

That works for me if I npm install @types/lodash and put the text _.add(1, 2); in Resources/a.ts.

@farfromrefug
Copy link
Author

Ok this is really weird. By moving things i got the same result as you.
My error might be that i try to extend LodashStatic
How would extend LodashStatic to add mixins functions definitions?

Thanks

@ghost
Copy link

ghost commented Aug 11, 2017

I think that issue is microsoft/TypeScript#17736.

@farfromrefug
Copy link
Author

@andy-ms thanks. Can we keep this open until there is a solution?

@ghost
Copy link

ghost commented Aug 14, 2017

OK

@farfromrefug
Copy link
Author

farfromrefug commented Aug 21, 2017

OK so after seeing the discussion in the issue you referenced i found a way to go it to work all the time for me.

First let me explain why it worked for you and not for me.
It worked for you because in the example file were not doing any import. Add an import and the global _ won't work.

Now what i did to get _ everywhere and to augment it i created a lodash.d.ts file in my project:

declare module "mod" {
    module "lodash" {
        interface LoDashStatic {
            remove<T, K extends keyof T>(
                object: T,
                key?: K,
                defaultValue?: T[K]
            ): T[K]
            getKeyByValue(object, value)
            mapIfDefined(array, func)
            mod(n, m)
            move(array, oldIndex, newIndex)
            pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>
        }
    }
}
declare var _: _.LoDashStatic;

Now i get global _ everywhere with my mixins

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

No branches or pull requests

1 participant