Skip to content

Type-check Import Attributes in static imports #55994

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
mkubilayk opened this issue Oct 5, 2023 · 1 comment · Fixed by #56034
Closed
5 tasks done

Type-check Import Attributes in static imports #55994

mkubilayk opened this issue Oct 5, 2023 · 1 comment · Fixed by #56034
Labels
Committed The team has roadmapped this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@mkubilayk
Copy link
Contributor

🔍 Search Terms

import attributes, import assertions

✅ Viability Checklist

  • 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 our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

TypeScript 5.3 Beta supports the latest updates to the import attributes proposal, which is great to see!

It's possible to augment the global ImportAttributes type with host-specific attributes. However, auto-complete and type-checking of import attributes seems to only work with dynamic import(). It would be nice if this could also work for attributes in static declarations.

📃 Motivating Example

https://www.typescriptlang.org/play?target=99&ts=5.3.0-dev.20231002#code/CYUwxgNghgTiAEBzCB7ARlC8DeBYAUPPAJYB2ALiDAGZRgLwCSAtgA4ozkCC55MxaAK6UAzjgJEi5AJ6sQALngAiAFYiUpJRPgBfAnvwFibDuXgAqeFDGkx1GCmbLqKFEvgB3YuQAWOeDJyikqkKOQAtGoa7joA3AQAbijEwPC28Yb4xuycABRKLm4ANOKEnt4+inhlUrIKyqERUZraBjoAlBkEQA

declare global {
    interface ImportAttributes {
        type: "json";
    }
}

import * as ns from "foo" with { type: "not-json" }; // no error
void ns;

import("foo", {
    with: {
        type: "not-json", // Type '"not-json"' is not assignable to type '"json"'. (2322)
    },
});

💻 Use Cases

  1. What do you want to use this for?

https://github.com/tc39/proposal-import-attributes#motivation

In addition, we have an internal use case in Bloomberg to use Import Attributes as an annotation to achieve lazy-loading by deferring module evaluation. This is an early implementation of the TC39 proposal for Deferring Module Evaluation that annotates the static import.

It would be a better developer experience if auto-complete was available for Import Attributes in the static form.

  1. What shortcomings exist with current approaches?

It is possible to write a static import declaration using unknown/invalid attributes.

  1. What workarounds are you using in the meantime?

None.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels Oct 5, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 6, 2023
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Oct 6, 2023
@RyanCavanaugh
Copy link
Member

Consensus from the design meeting was that we just "forgot" to do this.

Additional point for type authors - use caution when defining type like this. The correct way to make an "open-ended union", as should be used here, is to make a dummy interface and use keyof so that other declarations can introduce new items to the union:

  interface ImportAttributes {
    type: keyof AllowedTypes;
  }
  // In my .d.ts
  interface AllowedTypes {
    "json": unknown;
  }
  // In other .d.ts
  interface AllowedTypes {
    "png": unknown;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants