Skip to content

Shouldn't TypeScript be able to polyfill import defer with Proxy to work with "module": "commonjs"?Β #62031

Open
@KSXGitHub

Description

@KSXGitHub

πŸ” Search Terms

import defer commonjs
import defer require

βœ… Viability Checklist

⭐ Suggestion

Since the TypeScript beta announcement, I tried it in the playground. But setting "module" to "commonjs" sadly revealed that import defer only work with "module": "esnext" or "module": "preserve".

But Proxy can polyfill this exact behavior.

πŸ“ƒ Motivating Example

This code:

import defer * as MyModule from 'my-module'

should output something like this:

let _module
const MyModule = new Proxy({}, {
  get(_target, prop) {
    if (!_module) {
      _module = require('my-module') // the real code would obviously more complicated as it needs to force `_module` to be an object
    }
    return _module[prop]
  }
})

πŸ’» Use Cases

  1. What do you want to use this for? Using import defer for legacy targets
  2. What shortcomings exist with current approaches? await import forces Promise
  3. What workarounds are you using in the meantime? await import

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions