Skip to content

Feature Request: dynamic import with type parameter #43669

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
5 tasks done
shigma opened this issue Apr 14, 2021 · 9 comments
Closed
5 tasks done

Feature Request: dynamic import with type parameter #43669

shigma opened this issue Apr 14, 2021 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@shigma
Copy link

shigma commented Apr 14, 2021

Suggestion

πŸ” Search Terms

dynamic import

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Support dynamic import() with type parameter.

πŸ“ƒ Motivating Example

Assuming we are developing a framework which implements a CLI that accept such config file:

// xxx.config.ts
import { AppConfig } from 'libxxx'

export default {
  plugins: {
    'foo-plugin': { bar: 'baz' },
  },
} as AppConfig

will be equal to

import { App } from 'libxxx'
const app = new App()

// apply plugin with options
app.plugin(require('foo-plugin'), { bar: 'baz' })

The foo-plugin package can be like this:

import { App } from 'libxxx'

interface Config {
  bar: string
}

export function install(app: App, options: Config) {
  // implementation
}

But we can't get code hints from plugins, because we do not have typings support for dynamic imports.

πŸ’» Use Cases

// libxxx.d.ts
export interface AppConfig {
  plugins: {
    [K in string]: import(K) extends (app: App, options: infer T) => any ? T : never
  },
}
@NWYLZW
Copy link

NWYLZW commented Apr 14, 2021

++

@andrewbranch
Copy link
Member

Interesting idea, but there’s already an easier way to do this. Each plugin package can augment an interface from the main lib:

// foo-plugin:
import { App } from 'libxxx';

// Add 'foo-plugin' member to `PluginConfig` already declared in 'libxxx'
declare module 'libxxx' {
  interface PluginConfig {
    'foo-plugin': Config;
  }
}

interface Config {
  bar: string;
}

// ...

You can find lots of examples of this on DefinitelyTyped, e.g. for Express middleware packages. See how express-boom patches a property onto express’s Response interface.

@andrewbranch andrewbranch added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 14, 2021
@shigma
Copy link
Author

shigma commented Apr 15, 2021

@andrewbranch I know, but in your case, it seems that we have to import all the 'foo-plugin's into our config file, which is quite annoying......

// xxx.config.ts
import { AppConfig } from 'libxxx'

// if we didn't do this, ide would not show proper code hints
import {} from 'foo-plugin'
import {} from 'bar-plugin'
import {} from 'baz-plugin'

export default {
  plugins: {
    'foo-plugin': { bar: 'baz' },
  },
} as AppConfig

@andrewbranch
Copy link
Member

That’s a good point. I would suggest putting them in the types field of your tsconfig.json instead (I’m pro-manual-management of types), but, it’s the same amount of manual work.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 16, 2021

Seems like a duplicate of #26139 (and #42482) right?

@andrewbranch
Copy link
Member

I think you might have mistyped the number on the second one? But yeah, duplicate of #26139 at least in terms of the answer.

@andrewbranch andrewbranch added Duplicate An existing issue was already created and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 16, 2021
@DanielRosenwasser
Copy link
Member

Fixed to #42482.

@shigma
Copy link
Author

shigma commented Apr 17, 2021

Oh I see. It there possibility to introduce this feature in the future, or as is said in #26139 it was hard to fix?

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants