question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better typescript support for dependencies

See original GitHub issue

I’m going through the tutorial online and I’m not sure if this is a typescript thing or a gluegun thing. What I would like is to be able to define an extension:

// src/extensions/imdb-extension.ts

import { prompt } from 'gluegun/prompt'
import * as imdb from 'imdb-api'
import { GluegunToolbox } from 'gluegun'

export interface ImdbExtension {
  imdb: {
    getMovie: (movieName: string) => Promise<imdb.Movie>
  }
}

export default (toolbox: GluegunToolbox) => {
  // memoize the API key once we retrieve it
  // let imdbKey: string | false = false

  // get a movie
  async function getMovie(movieName: string): Promise<imdb.Movie> {
    const result = await prompt.ask({ type: 'input', name: 'key', message: 'API Key>' })
    if (!result.key) return

    return imdb.get({ name: movieName }, { apiKey: result.key, timeout: 30000 })
  }

  // attach our tools to the toolbox
  toolbox.imdb = { getMovie }
}

AND I would like this object to show up in my typescript autocompletions. The closest thing I could think of was creating the interface above and doing some gymnastics but I feel that the typesystem could support this natively somehow.

import { GluegunCommand, GluegunToolbox } from 'gluegun'
import { ImdbExtension } from '../extensions/imdb-extension'

const movieCommand: GluegunCommand = {
  name: 'movie',
  run: async (toolbox: GluegunToolbox) => {
    // ** Fails - Type 'GluegunToolbox' is not assignable to type 'ImdbExtension'.   Property 'imdb' is missing in type 'GluegunToolbox'. [2322]
    // const { imdb }: ImdbExtension = toolbox 

    // works
    const { imdb }: ImdbExtension = toolbox as any
    const movie = await imdb.getMovie('The Room')
    if (movie.name) toolbox.print.info(movie.name)
  }
}

export default movieCommand

What would be ideal

const movie = toolbox.imdb.getMovie("The Room")   <- just autocompletes because TS knows the type
if (movie.name) toolbox.print.info(movie.name)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ductiletoastercommented, Jul 27, 2019

I’ve been build out a workflow CLI for my company and ran into an issue with some of the typings. In retrospect I probably should have created a new ticket but I created this PR to help solve my issue. https://github.com/infinitered/gluegun/pull/564

I apologies again for hijacking this thread. Please let me know if you’d like me to create a new ticket.

0reactions
infinitered-circlecicommented, Jul 30, 2019

🎉 This issue has been resolved in version 3.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top 5 TypeScript dependency injection containers
Explore how to implicitly automate dependency management in TypeScript using a dependency injection container.
Read more >
Better JavaScript? Use TypeScript and Dependency Injection
The main purpose of TypeScript is to allow cleaner, more readable JavaScript code by adding types. It is an aid to developers, mostly...
Read more >
Dependency Injection in TypeScript | by Mert Türkmenoğlu
In the dependency injection principle, we need to understand four different types of roles: Client; Service; Interface; Injector. Services are ...
Read more >
Setting Up Dependency Injection with TypeScript in an Object ...
In this post, you will learn what dependency injection is, why it is useful, when to use it, and what instruments and tools...
Read more >
Dependency Injection in TypeScript - DEV Community ‍ ‍
Now the class doesn't use implicit dependencies. This is good, but this injection is still awkward: you have to add references to objects...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found