TypeScript types to external library - javascript

I have an external library without types. Looks something like that:
var method1 = require('./method1');
var method2 = require('./method1');
module.exports = {
method1: method1,
method2: method2
}
I created a directory "typings/lib-name"(added "typings" in typeRoots). In this folder I have index.d.ts file and there something like this:
declare module "lib-name" {
import { Method1Interface, ... } from "./modules/method1.d.ts";
import { Method2Interface, ... } from "./modules/method2.d.ts";
export {
Method1Interface,
Method2Interface,
...
}
}
And when I try to use it in my code:
import lib from "lib-name"
lib.method1()
I get: TS2339: Property 'method1' does not exist on type 'typeof import("lib-name")'
I just starting with TS so I would be grateful for explaining where I went wrong.
EDITED
I added default export in module:
declare module "lib-name" {
import { Method1Interface, ... } from "./modules/method1.d.ts";
import { Method2Interface, ... } from "./modules/method2.d.ts";
export {
Method1Interface,
Method2Interface,
...
}
default export {
method1: Method1Interface
}
}
But then method1 has type any. I don't understand why.
EDITED2
It works when I defined methods before default export.
declare module "lib-name" {
import { Method1Interface, ... } from "./modules/method1.d.ts";
import { Method2Interface, ... } from "./modules/method2.d.ts";
const method1: Method1Interface;
const method2: Method2Interface;
export {
Method1Interface,
Method2Interface,
...
}
default export {
method1,
method2
}
}

I resolved my issue. Firstly I didn't have a default export. Next, default export shouldn't use syntax like this:
default export {
method1: Method1Interface
}
I defined method1 before the default export and then all works fine.
declare module "lib-name" {
import { Method1Interface, ... } from "./modules/method1.d.ts";
import { Method2Interface, ... } from "./modules/method2.d.ts";
const method1: Method1Interface;
const method2: Method2Interface;
export {
Method1Interface,
Method2Interface,
...
}
default export {
method1,
method2
}
}

Related

TypeScript does not provide default export

I have problems with Laravel + Vite + Vue 3 project.
I have installed everything as documentation and needed, and this project works separated from Laravel and Vite. But here is the problem, TypeScript doesn't recognize export default. It's always giving an error like:
MainLayout.vue:42
Uncaught SyntaxError: The requested module '/resources/scripts/composable/Auth.js' does not provide an export named 'default' (at MainLayout.vue:42:1)
But the Auth.ts file has exported function, and it looks like:
export default function useAuth(){
return {
CurrentUserToken: 'test';
};
}
This is how I'm calling in some files (example)
import useAuth() from './Auth';
const { CurrentUserToken } = useAuth();
return CurrentUserToken;
Why it would not recognize this named function?
You can export it like this
export function useAuth() {
return {
CurrentUserToken: 'test';
};
}
Import
import { useAuth } from './Auth';
Execute the function
useAuth();
OR
If you want to export default
export default function() {
return {
CurrentUserToken: 'test';
};
}
And import would look like this
import useAuth from './Auth';
Execute the function
useAuth();

How to change module.exports to import and export?

module.exports = function (idx) {
this.camera = idx;
};
module.exports.CONFIG = function () {
return Promise.resolve([]);
};
module.exports.CONFIG.FLOOR = function () {
return Promise.resolve([]);
}
I have a file that contains code like above.
I require this file and console.log it. It only shows
function (idx) {
this.camera = idx;
}
Why other attributes are hidden?
And if I delete first module.exports paragraph and console.log it, it shows an anonymous function(or default function ?) in CONFIG.
{ CONFIG:
{ [Function]
FLOOR: [FUNCTION]
}
}
I am wondering how to change it to import/export type instead of module.exports/require?
Thanks!
It looks like you have both named exports and a default export. When exporting, that would look something like:
// Default export:
export default function (idx) {
this.camera = idx;
};
function CONFIG() {
return Promise.resolve([]);
}
CONFIG.FLOOR = function () {
return Promise.resolve([]);
}
// Named export:
export CONFIG;
Then, when importing them, you need to both import the default and the named:
import idxFn, { CONFIG } from '../foo';
^^^^^ default import
^^^^^^ named import
You'll then be able to access FLOOR by referencing CONFIG.FLOOR.
But, note that having a function which is a property of another function is really weird. You might consider exporting FLOOR as another named export instead, just like CONFIG:
// Default export:
export default function (idx) {
this.camera = idx;
};
// Named exports:
export function CONFIG() {
return Promise.resolve([]);
}
export function FLOOR () {
return Promise.resolve([]);
}

Export module class from another module

I have a module netmap that exports a default class NetMap:
export default class NetMap {...}
I have another module helloworld and I want to export (not as a default) the entire NetMap class so that another module can access NetMap by using:
import * as helloworld from 'helloworld'
const x = helloworld.NetMap()
Is this possible? What would the export of NetMap look like in the helloworld module?
netmap.js
export default class NetMap {
...
}
helloworld.js (usually called a barrel):
import NetMap from './netmap.js';
import Foo from '...';
import ...
export {
NetMap,
Foo,
...
};
Then, in another module:
import * as helloworld from './helloworld.js';
const x = new helloworld.NetMap();
But I personally prefer to use named imports/exports, so I would do it like this instead:
netmap.js
export class NetMap {
...
}
helloworld.js (usually called barrel):
export { NetMap } from './netmap.js';
export { Foo } from '...';
export { ...
Then, in another module:
import * as helloworld from './helloworld.js';
const x = new helloworld.NetMap();
Or:
import { NetMap } from './helloworld.js';
const x = new NetMap();
I think I can tell what you're trying to do, and it certainly seems possible. But do let me know if I misunderstood.
So you have your netMap file...
// netMap.js
class NetMap {
constructor(a,b) {
this.a = a
this.b = b
}
}
export default NetMap
then you have your helloworld file that uses netmap as well as maybe some other things....
// helloworld.js
const netMap = require('./netMap')
// import netMap from 'netMap'
const helloWorld = _ => console.log('hello world!')
module.exports = { netMap, helloWorld }
export { netMap, helloWorld }
and now you have a third file for which you're going to import all of hello world...
// otherModule.js
var helloWorld = require('./helloworld')
// import * as helloWorld from 'helloworld'
const x = new helloWorld.netMap(2,3)
console.log(x.a, x.b)

Export multiple classes in ES6 modules

I'm trying to create a module that exports multiple ES6 classes. Let's say I have the following directory structure:
my/
└── module/
├── Foo.js
├── Bar.js
└── index.js
Foo.js and Bar.js each export a default ES6 class:
// Foo.js
export default class Foo {
// class definition
}
// Bar.js
export default class Bar {
// class definition
}
I currently have my index.js set up like this:
import Foo from './Foo';
import Bar from './Bar';
export default {
Foo,
Bar,
}
However, I am unable to import. I want to be able to do this, but the classes aren't found:
import {Foo, Bar} from 'my/module';
What is the correct way to export multiple classes in an ES6 module?
Try this in your code:
import Foo from './Foo';
import Bar from './Bar';
// without default
export {
Foo,
Bar,
}
Btw, you can also do it this way:
// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
export { default } from './Baz'
// and import somewhere..
import Baz, { Foo, Bar } from './bundle'
Using export
export const MyFunction = () => {}
export const MyFunction2 = () => {}
const Var = 1;
const Var2 = 2;
export {
Var,
Var2,
}
// Then import it this way
import {
MyFunction,
MyFunction2,
Var,
Var2,
} from './foo-bar-baz';
The difference with export default is that you can export something, and apply the name where you import it:
// export default
export default class UserClass {
constructor() {}
};
// import it
import User from './user'
Hope this helps:
// Export (file name: my-functions.js)
export const MyFunction1 = () => {}
export const MyFunction2 = () => {}
export const MyFunction3 = () => {}
// if using `eslint` (airbnb) then you will see warning, so do this:
const MyFunction1 = () => {}
const MyFunction2 = () => {}
const MyFunction3 = () => {}
export {MyFunction1, MyFunction2, MyFunction3};
// Import
import * as myFns from "./my-functions";
myFns.MyFunction1();
myFns.MyFunction2();
myFns.MyFunction3();
// OR Import it as Destructured
import { MyFunction1, MyFunction2, MyFunction3 } from "./my-functions";
// AND you can use it like below with brackets (Parentheses) if it's a function
// AND without brackets if it's not function (eg. variables, Objects or Arrays)
MyFunction1();
MyFunction2();
#webdeb's answer didn't work for me, I hit an unexpected token error when compiling ES6 with Babel, doing named default exports.
This worked for me, however:
// Foo.js
export default Foo
...
// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
...
// and import somewhere..
import { Foo, Bar } from './bundle'
// export in index.js
export { default as Foo } from './Foo';
export { default as Bar } from './Bar';
// then import both
import { Foo, Bar } from 'my/module';
For multiple classes in the same js file, extending Component from #wordpress/element, you can do that :
// classes.js
import { Component } from '#wordpress/element';
const Class1 = class extends Component {
}
const Class2 = class extends Component {
}
export { Class1, Class2 }
And import them in another js file :
import { Class1, Class2 } from './classes';
you can do.
export{className, className and so on}
For exporting the instances of the classes you can use this syntax:
// export index.js
const Foo = require('./my/module/foo');
const Bar = require('./my/module/bar');
module.exports = {
Foo : new Foo(),
Bar : new Bar()
};
// import and run method
const {Foo,Bar} = require('module_name');
Foo.test();

Exported Function isn't a Function?

Not sure why it's not viewing this as a function:
impl.js
export default function(callback){
return callback();
};
test.js
import {myModule} from '../../src/impl.js'
import {expect} from 'chai';
const module = myModule;
describe('', () => {
it('should callback when resolve is invoked', () => {
module(resolve => {
resolve('test');
}).then(value => {
expect(value).to.equal('test');
});
});
});
Error: TypeError: module is not a function
module isn't a function because where it receives its value, myModule, also isn't a function.
And, that is because you aren't using quite the correct syntax to import the export default. For that, you'll want to remove the braces around myModule:
import myModule from '../../src/impl.js'
From MDN:
Syntax
import *defaultMember* from *"module-name"*;
With the braces, the import will match a particular export by its name.
import { myModule } from '...';
Corresponds to either:
export let myModule = ...;
export function myModule() { ... };
And, impl.js doesn't export anything actually named myModule.
You don't have an export with name myModule. You only have a default export.
Either use a named export
export function myModule() { ... }
or import the the module properly
import myModule from '...';
See the export documentation on MDN for more info.

Categories

Resources