wessberg / rollup-plugin-ts

A TypeScript Rollup plugin that bundles declarations, respects Browserslists, and enables seamless integration with transpilers such as babel and swc
MIT License
495 stars 33 forks source link

Default assigned generic type isn't generated #186

Open KingSora opened 2 years ago

KingSora commented 2 years ago

Reproduction

I have two files: index.ts and SomeElse.ts:

// index.ts
export * from "./SomeElse";
// SomeElse.ts
export type SomeGeneric<T extends {} = any> = [string, T];

The input for rollup is the index.ts file. Note: if the input file would be SomeElse.ts (so the re-export isn't happening) the output would be correct.

Not very familiar with repl, but here is a link: https://replit.com/@KingSora/rollup-plugin-ts-bug The setup is actually really small, please just re-create it on your local machine if needed. Run npm run build to build the output.

Expected Behavior

The type is exported correctly:

type SomeGeneric<T extends {} = any> = [string, T];
export { SomeGeneric };

Actual Behavior

The type is exported without the default assigned type any:

type SomeGeneric<T extends {}> = [
    string,
    T
];
export { SomeGeneric };
SagnikPradhan commented 2 years ago

@wessberg Do you know what or where the issue could be? I would be willing to fix it.

KingSora commented 2 years ago

@wessberg I would also be willing to fix it, if you could provide some insights of what you think where the issue could be. Your plugin is really good!

tihuan commented 2 years ago

It seems like the default type is preserved if you use typescript@4.6. None of 4.7.x worked, so probably something specific to 4.7.x?

ICodeMyOwnLife commented 2 years ago

I face the same issue too, with dependency versions: "rollup": "2.78.1", "rollup-plugin-ts": "3.0.2", "typescript": "4.7.4"

export type Dict<Value = string> = { [key: string]: Value };

was built to

type Dict<Value> = {
    [key: string]: Value;
};

The default type parameter was stripped away. It doesn't happen when running tsc.

tihuan commented 2 years ago

I face the same issue too, with dependency versions: "rollup": "2.78.1", "rollup-plugin-ts": "3.0.2", "typescript": "4.7.4"

export type Dict<Value = string> = { [key: string]: Value };

was built to

type Dict<Value> = {
    [key: string]: Value;
};

The default type parameter was stripped away. It doesn't happen when running tsc.

if you're blocked by this, try typescript 4.6. It worked for us!

ICodeMyOwnLife commented 2 years ago

Thanks @tihuan, as you suggested, but instead of downgrading, I upgraded to "typescript": "4.8.2" and now it works. But 4.8.2 version of TS seems to introduce quite many breaking changes and deprecation notes. I saw these notes when I built my project.

DeprecationWarning: 'updateImportTypeNode' has been deprecated since v4.6.0. Use the overload that accepts 'assertions'
DeprecationWarning: 'createTypeParameterDeclaration' has been deprecated since v4.7.0. Use the overload that accepts 'modifiers'
DeprecationWarning: 'isTypeAssertion' has been deprecated since v4.0.0. Use `isTypeAssertionExpression` instead.
DeprecationWarning: 'createConstructorTypeNode' has been deprecated since v4.2.0. Use the overload that accepts 'modifiers'