Repository: TypeScript
Issue: [Feature request] use Proxy in __importStar when target >= es6 with esModuleInterop
Labels: Needs Investigation

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Please read the FAQ first, especially the "Common Feature Requests" section.

-->

## Search Terms

<!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->

## Suggestion

use `Proxy` in `__importStar` when `target >= es6` with `esModuleInterop`

## Use Cases

`import * as xxxx from yyyy` with `esModuleInterop`

## Examples

```ts
import console from 'debug-color2';
import util from 'util';

util.inspect.defaultOptions.colors = true;

function __importStar(mod)
{
	if (mod && mod.__esModule) return mod;
	var result = {};
	if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
	result["default"] = mod;
	return result;
}

function __importStar2(mod)
{
	if (mod && mod.__esModule) return mod;

	let _type = typeof mod;

	if (mod == null || _type !== 'function' && _type !== 'object')
	{
		return {
			default: mod
		}
	}

	return new Proxy(mod, {
		get(target, property)
		{
			if (property === 'default')
			{
				return mod
			}

			return Reflect.get(mod, property);
		},
		has(target, property)
		{
			if (property === 'default')
			{
				return true
			}

			return Reflect.has(mod, property);
		},
	});
}

let _a1 = __importStar(require('./temp2'));

let _a2 = __importStar2(require('./temp2'));

let _a3 = require('./temp2');

print(_a1, 1);

print(_a2, 2);

print(_a3, 3);

function print(mod, n)
{
	console.log('----------');
	_dir(n);

	_dir(mod, `mod`);
	_dir(mod.v1, `mod.v1`);

	mod.notExistsProp = 777;

	try
	{
		_dir(mod.default(), `mod.default()`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}

	mod.v1++;

	_dir(mod.v1, `mod.v1`);

	try
	{
		_dir(mod.default(), `mod.default()`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}

	_dir(mod, `mod`);
	_dir(mod.default, `mod.default`);

	_dir(Object.keys(mod));

	_dir('default' in mod, `'default' in mod`);

	_dir('__esModule' in mod, `'__esModule' in mod`);
	_dir(mod.__esModule, `mod.__esModule`);
	_dir(mod.__cccc, `mod.__cccc`);

	try
	{
		_dir('__esModule' in mod.default, `'__esModule' in mod.default`);
		_dir(mod.default.__esModule, `mod.default.__esModule`);
		_dir(mod.default.__cccc, `mod.default.__cccc`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}

	try
	{
		_dir(mod(), `mod()`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}
}

function _dir(value, label?)
{
	if (typeof label !== 'undefined')
	{
		console.log(`[${label}]`, util.inspect(value));
	}
	else
	{
		console.log(util.inspect(value));
	}
}
```

```ts
import util from 'util';

function aaaa()
{
	console.log(`module.exports`, util.inspect(module.exports));

	return 1
}

aaaa.v1 = 777;

Object.defineProperty(aaaa, '__cccc', {
	value: true
});

export = aaaa

```

## Checklist

My suggestion meets these guidelines:

* [ ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [ ] This wouldn't change the runtime behavior of existing JavaScript code
* [ ] This could be implemented without emitting different JS based on the types of the expressions
* [ ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [ ] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).