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

export default as xxx issue #145

Closed
tianyingchun opened this issue Jan 11, 2021 · 5 comments
Closed

export default as xxx issue #145

tianyingchun opened this issue Jan 11, 2021 · 5 comments

Comments

@tianyingchun
Copy link

Bug report

Input code

// in `md5/index.ts`
import { b64_hmac_md5, b64_md5, hex_hmac_md5, hex_md5 } from './md5';
export default {
  hexMd5: hex_md5,
  b64Md5: b64_md5,
  hexHmacMd5: hex_hmac_md5,
  b64HmacMd5: b64_hmac_md5,
};
// index.ts
export { default as md5 } from './md5';
export { default as signMd5 } from './sign-md5';

Expected output

declare const md5: {
	hexMd5: typeof hex_md5;
	b64Md5: typeof b64_md5;
	hexHmacMd5: typeof hex_hmac_md5;
	b64HmacMd5: typeof b64_hmac_md5;
};
declare const signMd5: {
	signForGet: (signSecret: string, timestamp: number, reqQuery: Record<string, unknown>, extraSignStr?: string) => string;
	signForPost: (signSecret: string, timestamp: number, reqDataParam: Record<string, unknown>, extraSignStr?: string) => string;
};

Actual output

// in index.d.ts
declare const _default: {
	hexMd5: typeof hex_md5;
	b64Md5: typeof b64_md5;
	hexHmacMd5: typeof hex_hmac_md5;
	b64HmacMd5: typeof b64_hmac_md5;
};

declare const _default: {
	signForGet: (signSecret: string, timestamp: number, reqQuery: Record<string, unknown>, extraSignStr?: string) => string;
	signForPost: (signSecret: string, timestamp: number, reqDataParam: Record<string, unknown>, extraSignStr?: string) => string;
};

Additional context
Add any other context about the problem here (CLI options, etc)

@tianyingchun
Copy link
Author

BTW maybe we need to change

export default {
  hexMd5: hex_md5,
  b64Md5: b64_md5,
  hexHmacMd5: hex_hmac_md5,
  b64HmacMd5: b64_hmac_md5,
};

to

const md5 = {
  hexMd5: hex_md5,
  b64Md5: b64_md5,
  hexHmacMd5: hex_hmac_md5,
  b64HmacMd5: b64_hmac_md5,
};
export default md5

it works

@timocov
Copy link
Owner

timocov commented Jan 18, 2021

@tianyingchun Hi there, sorry for late reply. What is in './md5' module? It seems that you didn't provide it here (I'm asking it because it might help to recreate test-case and fix the issue).

@tianyingchun
Copy link
Author

for md5.ts it contains some helper methods


/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
export function hex_md5(s): string {
  return rstr2hex(rstr_md5(str2rstr_utf8(s)));
}

export function b64_md5(s): string {
  return rstr2b64(rstr_md5(str2rstr_utf8(s)));
}

export function hex_hmac_md5(k, d): string {
  return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
}

export function b64_hmac_md5(k, d): string {
  return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
}

@tianyingchun
Copy link
Author

@timocov is it a bug?

@timocov
Copy link
Owner

timocov commented Jan 24, 2021

Hi @tianyingchun, sorry for late reply (it was quite busy week though).

Am I right that the issue is that there is 2 declare const _default statements which lead to an error Cannot redeclare block-scoped variable '_default'? I mean, all other output is fine:

declare function hex_md5(): string;
declare function b64_md5(): string;
declare function hex_hmac_md5(): string;
declare function b64_hmac_md5(): string;
declare const _default: {
	hexMd5: typeof hex_md5;
	b64Md5: typeof b64_md5;
	hexHmacMd5: typeof hex_hmac_md5;
	b64HmacMd5: typeof b64_hmac_md5;
};
declare function signForGet(signSecret: string, timestamp: number, reqQuery: Record<string, unknown>, extraSignStr?: string): string;
declare function signForPost(signSecret: string, timestamp: number, reqDataParam: Record<string, unknown>, extraSignStr?: string): string;
declare const _default: { // Error: Cannot redeclare block-scoped variable '_default'
	signForGet: typeof signForGet;
	signForPost: typeof signForPost;
};

export {
	_default as md5,
	_default as signMd5,
};

export {};

If so, I'm afraid that this is #116 actually, because TypeScript compiler produces that _default variables itself and that's why there is 2 variables in the bundle with the same name.

As workaround you can use trick from #145 (comment) for sure.

@timocov timocov closed this as completed Jan 24, 2021
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

No branches or pull requests

2 participants