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

import inconsistent? #11025

Closed
BobFrankston opened this issue Sep 21, 2016 · 6 comments
Closed

import inconsistent? #11025

BobFrankston opened this issue Sep 21, 2016 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@BobFrankston
Copy link

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?

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Sep 21, 2016
@RyanCavanaugh
Copy link
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');

@BobFrankston
Copy link
Author

Thanks very much.

@BobFrankston
Copy link
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 = ..."

@BobFrankston BobFrankston reopened this Sep 21, 2016
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Sep 21, 2016

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.

@BobFrankston
Copy link
Author

BobFrankston commented Sep 21, 2016

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.

@BobFrankston
Copy link
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.

@BobFrankston BobFrankston reopened this Sep 22, 2016
@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants