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

Error when using const modifier in type parameters (Typescript 5 + Preact + Vite) #77

Closed
german664 opened this issue Jun 6, 2023 · 2 comments · Fixed by #82
Closed

Comments

@german664
Copy link

german664 commented Jun 6, 2023

Hi! I'm getting an error when running vite dev in a project with Preact + Vite + Typescript.

This issue happens when trying to use the new "Const type parameters" functionality available since Typescript 5.

Steps to reproduce:

  • Create a component with Preact + Vite + Typescript >5 + Preact plugin (from preset-vite).

  • Copy the following function:

    function testConstTypeParams<const T extends { name: string }>(props: T): T['name'] {
        return props.name
    };

    const name = testConstTypeParams({name: 'Han Solo'});

Issue

The code works and is accepted by TS compiler, but no by Vite.

The error in the console is the following:

[project] 2:59:59 PM [vite] Internal server error: /Users/user1/folder/project/src/components/internal/component.tsx: Unexpected keyword 'const'. (17:33)
[project] 
[project]       };
[project]   
[project] >     function testConstTypeParams<const T extends { name: string }>(props: T): T['name'] {
[project]                                     ^
[project]           return props.name
[project]       }
[project]   
[project]   Plugin: vite:preact-jsx
[project]   File: /Users/user1/folder/project/src/components/internal/component.tsx:17:33
[project]        };
[project]     
[project]         function testConstTypeParams<const T extends { name: string }>(props: T): T['name'] {
[project]                                       ^
[project]             return props.name
[project]         }

So it seems it's an issue with Plugin: vite:preact-jsx.

Tried to debug it and I think the culprit is the version of @babel/core being used. According to this PR the const modifier in type parametersis available since version 7.21.0, but I'm not really sure that upgrading the version will solve the issue.

I really hope that I was clear enough with my explanation. Please let me know if you need more info. Thanks!

@JoviDeCroock
Copy link
Member

Have you tried upgrading your version of @babel/core?

@TvojTatk0
Copy link

I have similar issue. Would like to use new keyword using from TS 5.2.0 but it is not possible (probably) due preset-vite plugin. Im getting this error:

Support for the experimental syntax 'explicitResourceManagement' isn't currently enabled

It would be really awesome to have updated dependencies in this great plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants