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

Unable to access types other than "Storage" (ex. "Bucket") from declaration files #379

Closed
mattwelke opened this issue Sep 11, 2018 · 4 comments
Assignees
Labels
api: storage Issues related to the googleapis/nodejs-storage API. priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@mattwelke
Copy link

mattwelke commented Sep 11, 2018

Environment details

  • OS: Ubuntu 18.04
  • Node.js version: 8.11.4
  • npm version: 6.4.1
  • @google-cloud/storage version: 2.0.1

Steps to reproduce

  1. Create a new Node project with TypeScript
  2. Install the latest version of google-cloud/storage.
  3. Try to use non-Storage types like Bucket by importing them. Ex. import { Bucket } from 'google-cloud/storage';.

Right now, despite multiple declaration files being present in the package's build directory, only index.d.ts is set by the package.json as a declaration file to be made available. Because of this, most of the types are unavailable. The only type I was able to access was the Storage class (to instantiate a client, etc).

Here is an example repo demonstrating the issue. The command npm build will fail. The comments marked "compiler error" show idiomatic imports from the library, should the types be made available:

https://github.com/welkie/issue-example-ts-google-cloud-storage

Note that the TypeScript documentation on publishing states that the types property in package.json should be set to a bundle of declarations: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

An alternative to adding a build step to concatenate the declaration files into a bundled file would be to export the imported types from the main file. For example:

// index.ts

import { Bucket } from './bucket';

// index stuff
class Storage {};

export { Bucket, Storage };

That way, all types can be imported from the main file like so:

// consumer.ts

import { Bucket, Storage } from '@google-cloud/storage';
@mattwelke
Copy link
Author

mattwelke commented Sep 11, 2018

Just to follow up, after tinkering more with it (version 2.0.2), I was able to import the non-index.d.ts types like so:

import { Storage } from '@google-cloud/storage';
import { Bucket } from '@google-cloud/storage/build/src/bucket';

const client = new Storage();

const bucket: Bucket = {} as any;

However, I don't think this is the conventional way to import types. Usually, you're meant to have the types available from the package itself without specifying subdirectories.

@JustinBeckwith JustinBeckwith added the triage me I really want to be triaged. label Sep 11, 2018
@ItalyPaleAle
Copy link

Sounds like the conversion to TS wasn't done properly... I'm having this issue too, and it's just one of the many (see also #384 )

@mattwelke
Copy link
Author

mattwelke commented Sep 12, 2018

I'd give them credit for doing the TS conversion to begin with. It's a big undertaking. I'm sure it will be resolved in time.

To any maintainers reading this, I recommend looking at TypeORM's repository as an example of a library implemented in TypeScript. You can see how they import everything into the index.ts file and then export it back out so that all types are available to consuming libraries.

@JustinBeckwith JustinBeckwith added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Sep 13, 2018
@JustinBeckwith JustinBeckwith removed the triage me I really want to be triaged. label Sep 13, 2018
@JustinBeckwith
Copy link
Contributor

Thanks for the patience folks :) Fixed in #484

@google-cloud-label-sync google-cloud-label-sync bot added the api: storage Issues related to the googleapis/nodejs-storage API. label Jan 31, 2020
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/nodejs-storage API. priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

5 participants