googleapis / nodejs-pubsub

Node.js client for Google Cloud Pub/Sub: Ingest event streams from anywhere, at any scale, for simple, reliable, real-time stream analytics.
https://cloud.google.com/pubsub/
Apache License 2.0
522 stars 227 forks source link

Wrong typescript signatures for methods returning Promise #376

Closed SkeLLLa closed 5 years ago

SkeLLLa commented 5 years ago

All methods that could return Promise have invalid typescript signatures.

For example, createTopic:

createTopic(name: string, gaxOpts: any, callback?: any): void;

First of all there's no override with Promise returned, but it should be. But for now usage of this lib in typescript is not possible with Promises or async-await, because

const topic = await pubsub.createTopic('myTopic');
// topic will have type void

The second thing - gaxOpts should be optional. And it would be good if it have proper interface, not just any.

Environment details

Steps to reproduce

  1. install @google-cloud/pubsub
  2. try to compile any method call that returns promise

Sample:

import {PubSub} from '@google-cloud/pubsub';
const pubsub = new PubSub({
  projectId, // provide id
  credentials // and credentials
});
const topic = await pubsub.createTopic(topicName, {})
// try to call any method from topic. or access property
SkeLLLa commented 5 years ago

@kinwa91 fyi there's a workaround to deal with this. First of all you need to cast result of function that suppose to return Promise to unknown type, and then convert it to type that actually should be returned. Example:

const tmp = plugin.topic.subscription(subName);
const [sub] = (<unknown>await tmp.get(
  {
    autoCreate: true,
  },
   undefined
)) as [Subscription];

But obviously it should have proper typings.

jkwlui commented 5 years ago

Yes, but I recommend that you use v0.22.0 for now that doesn't contain types information, while we finish up with converting to TypeScript. Thanks for your patience!

villeodell commented 5 years ago

@kinwa91 Doesn't v0.22.0 in fact contain type information?

JustinBeckwith commented 5 years ago

Greetings! We've done the initial conversion to TypeScript, but it is no way close enough to have high fidelity types. We're tracking actually turning type hinting on in #141 :)