Skip to content
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

[Experiment] feat(13626): Type annotations for default export #50536

Closed
wants to merge 4 commits into from

Conversation

a-tarasyuk
Copy link
Contributor

Fixes #13626

The compiler supports checking JsDoc type annotations for default exports (Playground, #47946), however, in TypeScript files, users are not allowed to add a type annotation to have this kind of validation.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 30, 2022
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #13626. If you can get it accepted, this PR will have a better chance of being reviewed.

@sandersn sandersn added this to Not started in PR Backlog Sep 12, 2022
@sandersn sandersn moved this from Not started to Waiting on author in PR Backlog Sep 21, 2022
@sandersn
Copy link
Member

From reading the issue, it sounds like there are open questions, at least for how to parse the type. Can you explain what the problem is and how this PR solves it? (If there are too many questions it might be better to hold off on this PR and settle them on the original issue.)

@sandersn sandersn self-assigned this Sep 21, 2022
@sandersn sandersn self-requested a review September 21, 2022 23:29
@a-tarasyuk
Copy link
Contributor Author

a-tarasyuk commented Sep 22, 2022

Can you explain what the problem is

Using JsDoc, we can use the type annotation for the default export like this

// @filename: /a.js
/** @type {string} */
export default 1;
               ^ Type 'number' is not assignable to type 'string'.

However, in TypeScript, we cannot directly add type annotation for the default export, only through alternative constructs like additional variables. Based on reactions and feedback, this limitation adds confusion (wat?) for users.

export default: number ...
              ^ Expression expected

and how this PR solves it?

This is a PoC that allows using type annotation for the export default in the following way

default export: <type> = <expression>

Why did I use =?
As far as I know, TypeScript ignores <space> in type annotations. The following examples are completely valid

const a: number   []   [] = [];
const b:   {}   [] = [];

I think it would be weird to disallow using <space> only in type annotations for export default.

export default: number   []   []   [];
                ^^^^^^^^^^^^^^^^

If there are too many questions

I think we need to find the best way to separate type annotation and expression.

  1. Can we use : to add type annotation?
    I think, yes. At least to follow the same user experience.
  2. Can we use = to separate type annotation and expression?
    Not sure.
  3. What about <>? However, this seems more of an assertion.
export default<number> 1; 
export default<number[]> []; 

@sandersn Do I need to add this answer to the issue and close PR? Maybe @RyanCavanaugh has thoughts about that case.

@sandersn
Copy link
Member

Do I need to add this answer to the #13626 and close PR?

I think so. The code changes are pretty small and the hard part is getting people to agree on the design, which is better done on an issue. Though, I don't know, maybe starting a new issue with your complete proposal would be easier to follow than appending it to the original.

@a-tarasyuk a-tarasyuk closed this Feb 14, 2023
PR Backlog automation moved this from Waiting on author to Done Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
PR Backlog
  
Done
Development

Successfully merging this pull request may close these issues.

Type annotations for default export
3 participants