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

Lost generic type argument from import() statements #178

Closed
Innei opened this issue Nov 30, 2021 · 2 comments
Closed

Lost generic type argument from import() statements #178

Innei opened this issue Nov 30, 2021 · 2 comments
Labels
Milestone

Comments

@Innei
Copy link

Innei commented Nov 30, 2021

Bug report

Hi, guys, I ran into two problems.

  1. generator can't bundle generic types correctly when I not import generic type before.
  2. path alias replace not working in import(...).

I don't know how to describe him, please see the following code.

Input code

The following examples are composed of three files, I will comment the file names. And I used path alias in tsconfig.json. e.g. ~/ redirect to src/

// interface/request.ts
// this file is shared for all case
export type RequestProxyResult<
  T,
  R = { data: T; [key: string]: any },
> = Promise<ResponseProxyExtraRaw<T, R>>

export type ResponseProxyExtraRaw<T, R = any> = T & {
  $raw: R
}
// utils/index.ts
// this file is shared for all case
import { RequestProxyResult } from '~/interfaces/request'

export const foo = (): RequestProxyResult<{}> => {
  // not care this, just force asserting a type
  return {} as any
}

case 1:

If the implicit type definition is not imported manually.

// index.case1.ts
import { foo } from '~/utils'
export const case1 = foo() // case1 type is RequestProxyResult<{}, {  [key: string]: any; data: {};}>, this type automatically inferred from ts

And run generator. It say build/index.d.ts(10,25): error TS2707: Generic type 'RequestProxyResult' requires between 1 and 2 type arguments. Lost generic type. The generated files are as follows

// Generated by dts-bundle-generator v6.1.0

export declare type RequestProxyResult<T, R = {
	data: T;
	[key: string]: any;
}> = Promise<ResponseProxyExtraRaw<T, R>>;
export declare type ResponseProxyExtraRaw<T, R = any> = T & {
	$raw: R;
};
export declare const case1: RequestProxyResult; // <------------- expected: RequestProxyResult<{}, {  [key: string]: any; data: {};}>

export {};

case 2:

// index.case2.ts
import { foo } from '~/utils'
import { ResponseProxyExtraRaw } from '~/interfaces/request' // <-------- Just import the type file, but not import required implicit type
export const case2 = foo()

dts Output:

// Generated by dts-bundle-generator v6.1.0

export declare type RequestProxyResult<T, R = {
	data: T;
	[key: string]: any;
}> = Promise<ResponseProxyExtraRaw<T, R>>;
export declare type ResponseProxyExtraRaw<T, R = any> = T & {
	$raw: R;
};
export declare const case2: import("~/interfaces/request").RequestProxyResult<{}, {             // <--------- import("~/") path alias not replace.
	[key: string]: any;
	data: {};
}>;

export {};

case 3:

This case is worked. Also the expected output.

// index.case3.ts

import { foo } from '~/utils'
import { RequestProxyResult } from '~/interfaces/request' //  <--------  import the type file, and import required implicit type, aka`RequestProxyResult`
export const case3 = foo() // aka: RequestProxyResult<...>

Output:

// Generated by dts-bundle-generator v6.1.0

export declare type RequestProxyResult<T, R = {
	data: T;
	[key: string]: any;
}> = Promise<ResponseProxyExtraRaw<T, R>>;
export declare type ResponseProxyExtraRaw<T, R = any> = T & {
	$raw: R;
};
export declare const case3: RequestProxyResult<{}, {  // <---------- worked.
	[key: string]: any;
	data: {};
}>;

export {};

Expected output

See above

Actual output

See above

Reproduce Repository

https://github.com/Innei/dts-reproduction

Additional context

  • TypeScript 4.4.4

Thanks.

@timocov
Copy link
Owner

timocov commented Dec 5, 2021

Hi there, sorry for late reply.

case 1:

This is a bug, I'll fix it.

case 2:

I'm afraid this is a bug of typescript compiler since it generates incorrect d.ts output. This statement import("~/interfaces/request") cannot work without your tsconfig so most likely this shouldn't be like that (it should be resolved to ./interfaces/request for example like in the case 1). Thus please create an issue in https://github.com/microsoft/TypeScript for this case.

@timocov timocov added the Bug label Dec 5, 2021
@timocov timocov changed the title incorrect path alias replace in import(...) and lost gerenic types Lost gerenic type argument from import() statements Dec 5, 2021
@timocov timocov added this to the 6.2 milestone Dec 5, 2021
@timocov timocov closed this as completed in c27c944 Dec 5, 2021
@timocov timocov changed the title Lost gerenic type argument from import() statements Lost generic type argument from import() statements Dec 5, 2021
@timocov
Copy link
Owner

timocov commented Dec 5, 2021

The fix for case 1 was just released in v6.2.0. Thanks for you report!

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

No branches or pull requests

2 participants