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

Wrong typings generated when import() is used for types #152

Closed
tianyingchun opened this issue Mar 31, 2021 · 8 comments · Fixed by #156
Closed

Wrong typings generated when import() is used for types #152

tianyingchun opened this issue Mar 31, 2021 · 8 comments · Fixed by #156
Labels
Milestone

Comments

@tianyingchun
Copy link

tianyingchun commented Mar 31, 2021

Bug report

Bad Typings code generated npx dts-bundle-generator src/index.ts -o index.d.ts
Sample demo is here

Input code

src/index.ts

export * from './fabric-utils';
export * from './fabric-x';

src/fabric-utils/index.ts

import { setDebugStatus } from './set-debug';

export const fabricUtils = {
  setDebugStatus,
};

src/fabric-utils/set-debug.ts

import { FabricDebugCallbackData, FabricDebugOptions } from './type.debug';

export const setDebugStatus = (options: FabricDebugOptions): FabricDebugCallbackData => {
  console.log('options', options)
  return {} as FabricDebugCallbackData;
};

src/fabric-utils/type.common.ts

export declare type Json = undefined | null | boolean | number | string | Json[] | {
	[prop: string]: Json;
};

export declare type FabricCallbackData<T extends Json = null> = {

   code: "fabric_catched_error" | string;
  
   data: T;
}

src/fabric-utils/type.debug.ts

import { FabricCallbackData } from './type.common';

export type FabricDebugOptions = {

  enableDebug: boolean;
};


export type FabricDebugCallbackData = FabricCallbackData<{

  enabled: boolean;
}>;

Expected output

// Generated by dts-bundle-generator v5.8.0

export declare type Json = undefined | null | boolean | number | string | Json[] | {
	[prop: string]: Json;
};
export declare type FabricCallbackData<T extends Json = null> = {
	
	code: "fabric_catched_error" | string;
	
	data: T;
};
export declare type FabricDebugOptions = {
	
	enableDebug: boolean;
};

export declare type FabricDebugCallbackData = FabricCallbackData<{
	
	enabled: boolean;
}>;
export declare const fabricUtils: {
	setDebugStatus: (options: FabricDebugOptions) =>  FabricDebugCallbackData;
};
export declare const fabricX: () => string;

export {};

Actual output

// Generated by dts-bundle-generator v5.8.0

export declare type Json = undefined | null | boolean | number | string | Json[] | {
	[prop: string]: Json;
};
export declare type FabricCallbackData<T extends Json = null> = {
	 
	code: "fabric_catched_error" | string;
	 
	data: T;
};
export declare type FabricDebugOptions = {
	 
	enableDebug: boolean;
};
 
export declare type FabricDebugCallbackData = FabricCallbackData<{
	 
	enabled: boolean;
}>;
export declare const fabricUtils: {
	setDebugStatus: (options: import("./type.debug").FabricDebugOptions) => import("./type.debug").FabricDebugCallbackData;
};
export declare const fabricX: () => string;

export {};

Additional context
typescript@^4.2.3, dts-bundle-generator@^5.8.0

@timocov
Copy link
Owner

timocov commented Apr 3, 2021

Am I right that the problem is that there are import("./type.debug") statements instead of actual types?

@tianyingchun
Copy link
Author

yes if i move these types into export file avoid import types it works fine. but i think import("./type.debug") is very common development experience. could you improve this behavior? allow us import typing from some individual typing files?

@Kilcekru
Copy link

Kilcekru commented Apr 5, 2021

I have a very similar issue
The interesting part is that the type itself is correctly exported, so FabricDebugCallbackData instead of import("./type.debug").FabricDebugCallbackData would immediately work

@tianyingchun
Copy link
Author

@Kilcekru yes, FabricDebugCallbackData has been exported!:)

@timocov
Copy link
Owner

timocov commented Apr 5, 2021

I'll try to fix it.

@timocov timocov added the Bug label Apr 5, 2021
@timocov timocov changed the title Wrong Typings Generated in index.d.ts Wrong typings generated when import() is used for types Apr 5, 2021
@timocov timocov added this to the 5.9 milestone Apr 10, 2021
@timocov
Copy link
Owner

timocov commented Apr 13, 2021

The new version 5.9 has been published with the fix.

@tianyingchun
Copy link
Author

:) great news again!

@tianyingchun
Copy link
Author

tianyingchun commented May 13, 2021

hi @timocov i have another question about this issue. it seems that it lose comments while import() is used for types
demo is here
while run npm run dts build it genereated an correct index.d.ts for typings. but it lose some important comments for method.

tsconfig.json with removeComment :false,
actual result is :

....
/**
 * comments for fabric utils.
 */
export declare const fabricUtils: {
	setDebugStatus: (options: FabricDebugOptions) => FabricDebugCallbackData;
};
export declare const fabricX: () => string;

export {};

expect result should be

....
 /**
 * comments for fabric utils.
 */
export declare const fabricUtils: {
  /**
   * comments for method `setDebugStatus`
   * @param options 
   * @returns 
   */
 setDebugStatus: (options: FabricDebugOptions) => FabricDebugCallbackData;
};
export declare const fabricX: () => string;

export {};

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

Successfully merging a pull request may close this issue.

3 participants