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

TypeScript 4.5 type modifiers on imported names are not supported. #902

Open
2 tasks done
danielweck opened this issue Jan 20, 2022 · 2 comments
Open
2 tasks done
Labels
bug Something isn't working upstream Related to or blocked on an upstream project

Comments

@danielweck
Copy link

Describe the bug

TypeScript 4.5 type modifiers on imported names are not supported.

https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#type-on-import-names

To Reproduce

For example, this works:

import type { Options, VNode } from 'preact';
import { options } from 'preact'; // eslint-disable-line no-duplicate-imports

...but this breaks:

import { type Options, type VNode, options } from 'preact';
SyntaxError: Error transforming /PATH/TO/code.ts: Unexpected token, expected ","

Expected behavior

Preact WMR should support all the tasty goodness of TypeScript syntactic sugar :)

Bug occurs with:

  • wmr or wmr start (development)
  • wmr build (production)
@danielweck danielweck added the bug Something isn't working label Jan 20, 2022
@rschristian rschristian added the upstream Related to or blocked on an upstream project label Jan 20, 2022
@rschristian
Copy link
Member

Issue to track in sucrase: alangpierce/sucrase#668 (comment)

@danielweck
Copy link
Author

danielweck commented Jan 20, 2022

FYI, I use this Preact WMR plugin to work around this problem:

config.plugins.push({
    name: 'TypeScript 4.5 type modifier on imported names removal plugin',
    enforce: 'pre',

    transform(code, id) {
        if (!/\.tsx?$/.test(id)) return;

        // TODO: is this really necessary in WMR's latest version?
        if (id[0] === '\0' || id[0] === '\b') return;

        if (config.mode === 'build' || config.mode === 'start') {
            const re = /^import(.*)({.+})\s*from\s*['|"](.+)['|"]/gm;
            return {
                code: code.replace(re, (match, $1, $2, $3) => {
                    if (!$2.includes('type ')) {
                        return match; // preserve as-is
                    }
                    return `import${$1}${$2.replace(/type /g, '')} from '${$3}'`;
                }),
                map: null,
            };
        }
    },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream Related to or blocked on an upstream project
Projects
None yet
Development

No branches or pull requests

2 participants