Skip to content

Commit 01b4e57

Browse files
authored
docs(readme): v0.1.1
1 parent 56e056e commit 01b4e57

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `std.module.format`
22

3-
> version 0.1.0
3+
> version 0.1.1
44
55
- [`std.module.format`](#-stdmoduleformat-)
66
* [Overview](#overview)
@@ -19,14 +19,19 @@
1919
+ [TypeScript auto-import](#typescript-auto-import)
2020
+ [Re-exporting](#re-exporting)
2121
+ [Dynamic Imports](#dynamic-imports)
22+
+ [ES Module Interop](#es-module-interop)
23+
* [Note](#note)
2224
+ [Needs two lines for non-class / non-function](#needs-two-lines-for-non-class---non-function)
2325
+ [React.js - Named Exports](#reactjs---named-exports)
24-
* [{} type](#---type)
26+
* [The {} type](#---type)
2527
+ [If you want a type that means "empty object"](#if-you-want-a-type-that-means--empty-object-)
2628
+ [If you are using React, and you want to define `type Props = {}`.](#if-you-are-using-react--and-you-want-to-define--type-props------)
2729
+ [`GenericObject`](#-genericobject-)
2830
+ [Module-related host hooks](#module-related-host-hooks)
2931

32+
<small><i><a href='#'>This is a work in progress</a></i></small>
33+
34+
3035
## Overview
3136

3237

@@ -281,6 +286,30 @@ const {HighCharts} \= await import('https://code.highcharts.com/js/es-modules/ma
281286
HighCharts.chart('container', { ... }); // Notice \`.default\`
282287
```
283288
289+
### ES Module Interop
290+
291+
292+
> [esModuleInterop, https://www.typescriptlang.org/tsconfig#esModuleInterop](https://www.typescriptlang.org/tsconfig#esModuleInterop)
293+
294+
By default (with esModuleInterop false or not set) TypeScript treats CommonJS/AMD/UMD modules similar to ES6 modules. In doing this, there are two parts in particular which turned out to be flawed assumptions:
295+
296+
a namespace import like `import * as moment from "moment"` acts the same as `const moment = require("moment")`
297+
298+
a default import like `import moment from "moment"` acts the same as `const moment = require("moment").default`
299+
300+
This mis-match causes these two issues:
301+
302+
- the ES6 modules spec states that a namespace import (import * as x) can only be an object, by having TypeScript treating it the same as = require("x") then TypeScript allowed for the import to be treated as a function and be callable. That’s not valid according to the spec.
303+
304+
- while accurate to the ES6 modules spec, most libraries with CommonJS/AMD/UMD modules didn’t conform as strictly as TypeScript’s implementation.
305+
306+
307+
##### Note
308+
309+
The namespace import `import * as fs from "fs"` only accounts for properties which are **owned** (basically properties set on the object and not via the prototype chain) on the imported object. If the module you’re importing defines its API using inherited properties, you need to use the default import form (`import fs from "fs"`), or *disable esModuleInterop*.
310+
311+
312+
284313
### Needs two lines for non-class / non-function
285314
286315
Can be one statement for function / class e.g.
@@ -336,14 +365,12 @@ import React, { lazy } from 'react';
336365
const MyComponent = lazy(() => import("./MyComponent.js"));
337366
```
338367
339-
340368
## {} type
341369
342370
> [Below is copied from this comment, see https://github.com/typescript-eslint/typescript-eslint/issues/2063#issuecomment-675156492](https://github.com/typescript-eslint/typescript-eslint/issues/2063#issuecomment-675156492)
343371
344372
We will not be removing `{}` from the rule defaults, as it is an unsafe type because it doesn't work how people think it works, and in most cases it allows weakly typed code.
345373
346-
---
347374
348375
This is the exact reason that the rule bans the `{}` ***type***.
349376
It's a common misconception that the `{}` ***type*** is the same as the `{}` ***value***.
@@ -355,7 +382,7 @@ This is obviously a huge type safety hole!
355382
356383
For example - the following code is _completely type-check valid_, even though it might make no sense to be:
357384
358-
```ts
385+
```typescript
359386
interface AAA {
360387
aaa: {};
361388
}
@@ -376,7 +403,7 @@ There are the following options for you:
376403
377404
You can use a type similar to this type.
378405
379-
```ts
406+
```typescript
380407
type EmptyObject = Record<string, never>; // or {[k: string]: never}
381408

382409
const a: EmptyObject = { a: 1 }; // expect error
@@ -414,11 +441,9 @@ You can use the following config to allow it:
414441
415442
Consider using [an eslint overrides config](https://eslint.org/docs/user-guide/configuring#configuration-based-on-glob-patterns) to limit the scope of this change to just react component files, to help ensure you're keeping your codebase as safe as possible.
416443
417-
----
418-
419444
As an aside - it's worth noting that `{}` is a very weird anomaly in TypeScript, because there is just one case where it actually does mean something akin to "empty object"; in an intersection type.
420445
421-
```ts
446+
```typescript
422447
type T1 = { a: 1 } & {};
423448
const t11: T1 = { a: 1 };
424449
const t12: T1 = true; // expected error
@@ -480,7 +505,7 @@ A module map is a [map](https://infra.spec.whatwg.org/#ordered-map) keyed by [tu
480505

481506
Since [module maps](https://html.spec.whatwg.org/multipage/webappapis.html#module-map) are keyed by (URL, module type), the following code will create three separate entries in the [module map](https://html.spec.whatwg.org/multipage/webappapis.html#module-map), since it results in three different (URL, module type) [tuples](https://infra.spec.whatwg.org/#tuple) (all with "`javascript`" type):
482507

483-
```
508+
```typescript
484509
import "https://example.com/module.mjs";
485510
import "https://example.com/module.mjs#map-buster";
486511
import "https://example.com/module.mjs?debug=true";
@@ -490,7 +515,7 @@ That is, URL [queries](https://url.spec.whatwg.org/#concept-url-query) and [frag
490515

491516
In contrast, the following code would only create a single entry in the [module map](https://html.spec.whatwg.org/multipage/webappapis.html#module-map), since after applying the [URL parser](https://url.spec.whatwg.org/#concept-url-parser) to these inputs, the resulting [URL records](https://url.spec.whatwg.org/#concept-url) are equal:
492517

493-
```
518+
```typescript
494519
import "https://example.com/module2.mjs";
495520
import "https:example.com/module2.mjs";
496521
import "https://///example.com\\module2.mjs";
@@ -503,7 +528,7 @@ Note that this behavior is the same as how [shared workers](https://html.spec.wh
503528

504529
Since module type is also part of the [module map](https://html.spec.whatwg.org/multipage/webappapis.html#module-map) key, the following code will create two separate entries in the [module map](https://html.spec.whatwg.org/multipage/webappapis.html#module-map) (the type is "`javascript`" for the first, and "`css`" for the second):
505530

506-
```
531+
```html
507532
<script type=module>
508533
import "https://example.com/module";
509534
</script>
@@ -520,7 +545,7 @@ The purpose of including the type in the [module map](https://html.spec.whatwg.o
520545

521546
JavaScript module scripts are the default import type when importing from another JavaScript module; that is, when an `import` statement lacks a `type` import assertion the imported module script's type will be JavaScript. Attempting to import a JavaScript resource using an `import` statement with a `type` import assertion will fail:
522547
523-
```
548+
```html
524549
<script type="module">
525550
// All of the following will fail, assuming that the imported .mjs files are served with a
526551
// JavaScript MIME type. JavaScript module scripts are the default and cannot be imported with
@@ -569,3 +594,8 @@ The following are not valid module specifiers according to the above algorithm:
569594
- `.tomato`
570595
- `..zucchini.mjs`
571596
- `.\yam.es`
597+
598+
599+
## License
600+
601+
CC-SA-2.5

0 commit comments

Comments
 (0)