Skip to content

import inconsistent? #11025

Closed
Closed

Description

TypeScript Version: 1.8.36.0

Code

import "http2"
- no error message

import http2 from "http2"
Severity    Code    Description Project File    Line    Suppression State
Error       Build:Cannot find module 'http2'.   xp3starter  Y:\dev\projects\Site\xp3b\xp3starter\app.ts 14  
Error   TS2307  Cannot find module 'http2'. xp3starter  Y:\dev\projects\Site\xp3b\xp3starter\app.ts 14  Active

var http2 = require("http2");
- compiles

Expected behavior:

Actual behavior:

This may be my confusion. I've added the http2 module with npm. The first format does not produce an error (though is not very useful). The second form does.

This may be my confusion. My sense is that import works if I have a definitely typed file. But haven't found the exact documentation. I do have a related question - how do I say that http2 should use the http module definition?

Activity

  1. RyanCavanaugh commented on Sep 21, 2016

    @RyanCavanaugh
    Member
    import "http2"
    

    This is what we generally call a "forced" import. It doesn't create a local binding, so there's no reason to force it to resolve to a known module. These are usually used to load non-code resources anyway.

    import http2 from "http2"
    

    There's no definition for the "http2", so we don't know how to resolve the type of the local http2 binding

    var http2 = require("http2");

    This is using the underlying module loader.

    You can write this to get the behavior you want

    import _http = require('http'); // don't use _http in any other place
    var http2 = <typeof _http>require('http2');
  2. BobFrankston commented on Sep 21, 2016

    @BobFrankston
    Author

    Thanks very much.

  3. BobFrankston commented on Sep 21, 2016

    @BobFrankston
    Author

    Reopening this. First, the <typeof ..> notation is not at all obvious until you see it. Thanks.

    But now in using "http.

    import http1 = require("http");
    var http = <typeof http1>require("http2");
       ServerResponder(req: http.ServerRequest, response: http.ServerResponse) {
    
    Error   TS2503  Cannot find namespace 'http'.
    
    

    same for "var http2 = ..."

  4. RyanCavanaugh commented on Sep 21, 2016

    @RyanCavanaugh
    Member

    Sorry this is a bit confusing -- you'll want to use http1 in namespace positions (places where a dotted name produces a type), e.g. http1.ServerRequest. #10187 would create a way to do this unconfusingly by letting you alias the namespace side of http1.

  5. BobFrankston commented on Sep 21, 2016

    @BobFrankston
    Author

    Thanks. Yeah -- getting to the truth by lying. I'll have to take the time go through the aliasing procedure given the overlapping mechanisms. And, more generally,how to get the declarations for importing JavaScript but that's probably best for the suggestions forum.

  6. BobFrankston commented on Sep 22, 2016

    @BobFrankston
    Author

    OK, gets stranger. Turns out that https://github.com/molnarg/node-http2 has an index.d.ts file and even mentions Typescript 1.8 compatibility. ! But it's unclear how to use it. My gut feel is that it should be found automatically. So I explicitly added

    /// <reference path="./node_modules\http2\typings\globals\node\index.d.ts" />
    

    which generates declaration errors.

    I feel this is close but so far.

  7. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions