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 types in JS with var x = require('./mod') #22161

Merged
merged 2 commits into from
Mar 8, 2018

Conversation

sandersn
Copy link
Member

Fixes #11825

Previously, in Javascript, you could reference types by importing them from a Typescript file:

// Filename: types.d.ts
export type Type = { a, b, c }

// Filename: use.js
import * as mod from './mod'
/** @type {mod.Type} */
var t;

But this didn't work with var = require('./mod') syntax. Now it does:

// Filename: types.d.ts
export type Type = { a, b, c }

// Filename: use.js
var mod = require('./mod')
/** @type {mod.Type} */
var t;

You can also now use the types of classes, both from Typescript and Javascript:

// Filename: pg.js
export class Pool {
  m() {
  }
}

// Filename: use.js
var pg = require('./pg')
/** @type {pg.Pool} */
var pool;

@sandersn sandersn requested a review from mhegazy February 24, 2018 00:01
@sandersn
Copy link
Member Author

@aozgaa @DanielRosenwasser you were interested in this PR too I believe.

@sandersn sandersn changed the title Import types in JS with var x = require('./mod') Import types in JS with var x = require('./mod') Feb 24, 2018
// i.e class C extends foo()./*do language service operation here*/B {}
return undefined;
}
const left = name.kind === SyntaxKind.QualifiedName ? name.left : name.expression;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a drive-by fix — you can see that the else clause will never happen based on the containing if.

@sandersn sandersn merged commit e4610e3 into master Mar 8, 2018
@sandersn sandersn deleted the js-require-imports-types branch March 8, 2018 19:11
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants