Skip to content

How can I let JSDoc reference to type in another module #8237

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

Closed
Thaina opened this issue Apr 21, 2016 · 18 comments
Closed

How can I let JSDoc reference to type in another module #8237

Thaina opened this issue Apr 21, 2016 · 18 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Thaina
Copy link

Thaina commented Apr 21, 2016

I have some d.ts files in my project and any files without module could be reference any class inside it directly. But some files is declare module and cannot be referred to it

This module also contain - sign in its name (it is aws-sdk from aws-sdk.d.ts)

@Thaina
Copy link
Author

Thaina commented Apr 21, 2016

Just found that reference module just work fine. It just cannot reference module name with - sign in its name and awssdk module using declare module "aws-sdk"

Was JSDoc support reference module by ID?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2016

I am having hard time piecing together the repro. Can you provide a self-contained sample we can look at?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Apr 21, 2016
@Thaina
Copy link
Author

Thaina commented Apr 21, 2016

@mhegazy

I use tsd to pull aws-sdk.d.ts into typings folder

And then as you can see. the code in aws-sdk.d.ts is declare module "aws-sdk" { }

The module name is aws-sdk which is not possible to use at @type or @param for jsdoc

What I want is something like

/** @params {aws-sdk.DynamoDB} dynamo */
function DoWork(dynamo) {
    // dynamo will reference to DynamoDB class defined in aws-sdk.d.ts and can get intellisense
}

Current workaround I use is rename it in aws-sdk.d.ts as declare module awssdk { } but it not consistence with the original tsd

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2016

the module name shoudl not be used as an identifier. it works for some package, e.g. react, as they define a global variable React that has the same shape as the module. aws-sdk does not have that, so you need to import it.

import * as AWS from "aws-sdk";


/** @param {AWS.DynamoDB} dynamo */
function DoWork(dynamo) {

}

@mhegazy mhegazy added Question An issue which isn't directly actionable in code and removed Needs More Info The issue still hasn't been fully clarified labels Apr 21, 2016
@Thaina
Copy link
Author

Thaina commented Apr 21, 2016

@mhegazy That's work. But I still need to use it on node server under ES5 (AWS Lambda latest node version is nodejs 4) it cause error with reserved keyword import

So I try to find a way to have it work with only jsdoc comment

Also I have seen there are syntax like this

declare module A {
    export = a;
}

So I made my own ts file and write

import * as sdk from "aws-sdk"
declare module AWSSDK {
    export = sdk;
}

but it still didn't work

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2016

So aws-sdk can be used without being imported? if so this is an error in the declaration file, and should be reported on definitely typed.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2016

For workarounds, neither or these would work. you want to use the new UMD module definition, (added in TS 2.0) #7125:

/// file: my-aws-sdk.d.ts
export * from "aws-sdk"
export as namespace AWS;
/// file: app.js

/// <reference path="my-aws-sdk.d.ts" />

/** @param {AWS.DynamoDB} dynamo */
function DoWork(dynamo) {

}

@Thaina
Copy link
Author

Thaina commented Apr 21, 2016

@mhegazy No it's not. I mean I already try using import and it make intellisense work. But it cannot use in my target server so I can't use your solution

That's that. so I need to wait for TS 2.0

Thank you very much for your help

@mhegazy mhegazy closed this as completed Apr 21, 2016
@Thaina
Copy link
Author

Thaina commented Apr 21, 2016

@mhegazy Also I would like to suggest that, in addition to yours export as namespace syntax. There should be something like /** @import a as b */ for pure JSDoc style too

@tjtaill
Copy link

tjtaill commented May 9, 2017

Is there any support to pull in a type defined in a jsdoc from another file yet ?
file1.js
/** @typedef {Object} TypeA */

I want to use that type in another file file2.js how can I do that in typescript ?

@mhegazy
Copy link
Contributor

mhegazy commented May 9, 2017

Tracked by #14377

@OliverJAsh
Copy link
Contributor

Like @tjtaill, I also want to be able to import @typedef definitions from other modules. Do we need to create a second issue to track @tjtaill’s request, or is that covered by #14377?

@Robula
Copy link

Robula commented Sep 15, 2017

I am trying to do something similar, I am using a jsconfig.json file and trying to set up a Karma configuration.

karma.conf.js

/// <reference types="karma" />

/**
 * @param {Config} [config]
 */
function setConfig(config) {
    config.set({
        // configuration...
    });
}

VSCode just says [js] Cannot find name 'Config'. I have tried @param {karma.Config} as well but no joy. I am either doing something wrong or this behaviour isn't supported yet.

@pulasthibandara
Copy link

^ I tried above with no luck.

@dinoboff
Copy link

How would you reference something like firebase-admin type definition?

https://unpkg.com/[email protected]/lib/index.d.ts

It includes those definitions:

// firebase-admin type definition
import {Bucket} from '@google-cloud/storage';
import * as _firestore from '@google-cloud/firestore';

declare namespace admin {
  interface AppOptions {
    credential?: admin.credential.Credential;
    databaseAuthVariableOverride?: Object;
    databaseURL?: string;
    storageBucket?: string;
  }

  function app(name?: string): admin.app.App;
}

declare namespace admin.app {
  interface App {
    name: string;
    options: admin.AppOptions;
  }
}

declare namespace admin.firestore {
  export import FieldPath = _firestore.FieldPath;
}

declare module 'firebase-admin' {
}

export = admin;

If i try to bridge with:

// lib/admin.d.ts
export * from 'firebase-admin';
export as namespace admin;

If I reference lib/admin.d.ts, and then type to use admin.app.App, tsc complains with "error TS2694: Namespace '"lib/admin"' has no exported member 'app'".

// lib/index.js
// @ts-check
/// <reference path="./admin.d.ts" />

'use strict';

const admin = require('firebase-admin');

/**
 * @type {admin.app.App|null}
 */
let defaultApp = null;

@pocesar
Copy link

pocesar commented Jan 24, 2018

for some people trying to use TS typings in plain JS, using JSDoc, sometimes I need to double export for it to work, like so:

import * as winston from 'winston'
export = winston
export as namespace winston

if you don't use export = winston, everything becomes any. if you omit the export as namespace, you get Cannot find namespace 'winston'. so a code like this can now work:

/// <reference path="my-winston.d.ts" />
/**
 * @type {winston.LoggerInstance | null}
 */
var logger = null

@dimkir
Copy link

dimkir commented Feb 5, 2018

Based on @mhegazy answer, but with plain requires and avoiding dash in aws-sdk namespace.

image

const AWS = require('aws-sdk');

/**
 * @param {AWS.DynamoDB} dynamo 
 */
module.exports = function(dynamo){
    return handler;

    function handler(event, context, callback){
        let msg = `We are running ${event.name}`;
        console.log(msg)
        callback(null, { msg });

    }    
}

@dinoboff
Copy link

dinoboff commented Feb 5, 2018

An ES6 import of types or a reference do not import anything at run time however.

Even with that example where the caller would have to import that package anyway, a test might want to avoid any side effect of the import.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 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

9 participants