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

module declaration and conditional require #15902

Closed
farfromrefug opened this issue May 17, 2017 · 9 comments
Closed

module declaration and conditional require #15902

farfromrefug opened this issue May 17, 2017 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@farfromrefug
Copy link

Yesterday i was doing this which was working great:

declare module 'noble' {
    export type State = "unknown" | "resetting" | "unsupported" | "unauthorized" | "poweredOff" | "poweredOn";
    export var state: State

    export interface Peripheral {
        type: string
    }

    export interface Characteristic {
        subscribe(callback?: (error: string) => void): void;
    }
}
import * as noble from 'noble'

Now i need to transform import * as noble from 'noble' into:

let noble = require('noble');
if (/win/.test(process.platform)) {
    noble = require('noble-uwp');
}

Both require actually use the same declaration. However i can't find a way to tell typescript that noble is of 'type' noble(the module)

Is there any way to do this with typescript?

THanks

@ghost
Copy link

ghost commented May 17, 2017

Try doing it without require().

import * as nobleA from "noble";
import * as nobleB from "noble-uwp";
const noble = /win/.test(process.platform) ? nobleB : nobleA;

@farfromrefug
Copy link
Author

@andy-ms but the issue is that the import of "noble-uwp" wont work on macos as it is a windows module.
So i would need to do it within a try/catch.
But i can't do an import within a try/catch.

That's why i am looking at declaring the module and using it as a "type"

@blakeembrey
Copy link
Contributor

@farfromrefug You can use imports for the type and then use the types-only, not the value (e.g. https://github.com/TypeStrong/ts-node/blob/b751a56e8a094d6905e47e4c96ab43a931bf4cb8/src/index.ts#L180). You might want to prefix noble with an _ or something to avoid accidentally using it though since there's no type-only imports with TypeScript.

import * as noble from 'noble'

const x: typeof noble = require('noble')

@mhegazy
Copy link
Contributor

mhegazy commented May 17, 2017

Full support for dynamic imports will come with import(..) statement support, see #14495 for more details.

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 17, 2017
@farfromrefug
Copy link
Author

@blakeembrey i tried what you suggest by doing this:

declare module 'noble' {
    export type State = "unknown" | "resetting" | "unsupported" | "unauthorized" | "poweredOff" | "poweredOn";
    export var state: State

    export interface Peripheral {
        type: string
    }

    export interface Characteristic {
        subscribe(callback?: (error: string) => void): void;
    }
}
import * as _noble from 'noble';
let noble: typeof _noble = require('noble');
if (/win/.test(process.platform)) {
    noble = require('noble-uwp');
}

If i do noble. the autocompletation gives me access to the interfaces within the module
However when i do

interface Peripheral extends noble.Peripheral {
}

i get an error can't find namespace 'noble'
I think this is because noble is seen as a variable and not a module/namespace?
Is there a way around this?

@mhegazy thanks for the heads up. This will take quite some time i guesss?

@kitsonk
Copy link
Contributor

kitsonk commented May 18, 2017

It is targeted for 2.4 at the moment, which isn't forever in the future. Next major release.

@ghost
Copy link

ghost commented May 18, 2017

@farfromrefug You can use _noble.Peripheral.
For ease of use you might want to make a myNoble module with a .d.ts of export * from "noble"; and handle the choosing between modules in there. That way you won't have to repeat that code everywhere.

@farfromrefug
Copy link
Author

@andy-ms i can't use _noble.Peripheral in the case where i am requiring 'noble-uwp'. I don't think it would work.
However your idea with the .d.ts might work. Will look at that. Thanks

@mhegazy
Copy link
Contributor

mhegazy commented Jun 2, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Jun 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants