clearwater
clearwater

Reputation: 665

In VSCode when exporting functions: "Individual declarations must be all exported or all local"

I recently upgraded to Visual Studio Code 0.5.0 and some new errors cropped up that weren't there before.

I have a bunch of functions that are declared locally and then exported. Since the upgrade, however, hovering over each of the local function names produces the error Individual declarations in merged declaration functionName must be all exported or all local.

This is an example local function that is exported.

var testParamsCreatorUpdater = function (lTestParams, creatorID){
    lTestParams.creator = creatorID;
    return lTestParams;
};
module.exports.testParamsCreatorUpdater = testParamsCreatorUpdater;

I realize I can change this to...

module.exports.testParamsCreatorUpdater = function (lTestParams, creatorID){
    lTestParams.creator = creatorID;
    return lTestParams;
};

And prepend module.exports. to every testParamsCreatorUpdater() call.

But why is the first snippet wrong? As I understand it, require() makes everything in the module.exports object available to whatever required it.

Upvotes: 23

Views: 25773

Answers (5)

kub1x
kub1x

Reputation: 3582

So DuckDuckGo got me here searching for the exact same error, but in 2022. I didn't find the exact reason so posting as an update and for completeness.


import { Something, SomethingElse } from './some/path';
import { ref } from 'vue';

// Many lines of code

function doTheStuff() {
  // The declarations were previously just local variables ...
  // const Something = ref<Something>();
  // const SomethingElse = ref<SomethingElse>();
}

// ... but then I decided to export them and got the error
export const Something = ref<Something>();
export const SomethingElse = ref<SomethingElse>();

You simply can not import Something as a type and then export Something variable as a value of a kind (here a vue reference object). However, you can name a local variable the same name as the type, like I originally had. It is the import/export combination where things got broken. Solution for me was to locally rename the types:

import { 
  Something as SomethingType, 
  SomethingElse as SomethingElseType
} from './some/path';
import { ref } from 'vue';

// ...

// No naming conflict anymore
export const Something = ref<SomethingType>();
export const SomethingElse = ref<SomethingElseType>();

Upvotes: 1

ralphchan
ralphchan

Reputation: 411

I think it's related to the feature of merged declaration for TypeScript ref. I have not done the detailed research for Typescript but it seems that it can include Javascript in the Typescript file.

I guess the way testParamsCreatorUpdater was declared in the Javascript was detected to be error by VSCode because it thinks the two declarations cannot be merged.

Upvotes: 0

nick
nick

Reputation: 698

I had this issue in Webstorm , I Restarted it and it went away

Upvotes: 7

Behnam
Behnam

Reputation: 683

You are exporting a variable in this file which is imported in the same file module (locally).

Upvotes: 1

leoncc
leoncc

Reputation: 233

I think at a JavaScript level it cannot differentiate between:

var testParamsCreatorUpdater = ...

and

module.exports.testParamsCreatorUpdater = ...

as the names are the same. I got the exact same error (leading me to this post) in TypeScript when I tried this:

import { AuditService } from '../services/audit.service';
import { Audit } from '../models/audit.model';

@Component({
    selector: 'audit',
    templateUrl: './audit.component.html',
})
export class Audit {
    constructor(private auditService: AuditService) {
    }
}

So TypeScript did not like that I imported a module called Audit and exported a class also called Audit.

Upvotes: 5

Related Questions