Skip to content

Commit 0d93d6b

Browse files
committed
TypeScript type defintions docs for website
1 parent b0d7a0d commit 0d93d6b

File tree

9 files changed

+146
-7
lines changed

9 files changed

+146
-7
lines changed

docs/web/docs/features/web-standards/base64-utility-methods.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ function btoa(data: string): string;
1212
```
1313

1414
## [Entry points]({docs-version}/docs/usage#h-entry-points)
15-
```ts
15+
```plaintext
1616
core-js(-pure)/stable|actual|full/atob
1717
core-js(-pure)/stable|actual|full/btoa
1818
```
1919

20+
## [TypeScript type definitions]({docs-version}/docs/typescript-type-definitions)
21+
[`@core-js/types/web/atob-btoa`](https://github.com/zloirock/core-js/blob/v4-types/packages/core-js-types/src/base/web/atob-btoa.d.ts)
22+
2023
## Examples
2124
```js
2225
btoa('hi, core-js'); // => 'aGksIGNvcmUtanM='

docs/web/docs/features/web-standards/iterable-dom-collections.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,17 @@ class [DOMTokenList, NodeList] {
5050
```
5151

5252
## [Entry points]({docs-version}/docs/usage#h-entry-points)
53-
```
53+
```plaintext
5454
core-js(-pure)/stable|actual|full/dom-collections/iterator
5555
core-js/stable|actual|full/dom-collections/entries
5656
core-js/stable|actual|full/dom-collections/keys
5757
core-js/stable|actual|full/dom-collections/values
5858
core-js/stable|actual|full/dom-collections/for-each
5959
```
6060

61+
## [TypeScript type definitions]({docs-version}/docs/typescript-type-definitions)
62+
[`@core-js/types/web/iterable-dom-collections`](https://github.com/zloirock/core-js/blob/v4-types/packages/core-js-types/src/base/web/iterable-dom-collections.d.ts)
63+
6164
## Examples
6265
```js
6366
for (let { id } of document.querySelectorAll('*')) {

docs/web/docs/features/web-standards/queuemicrotask.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ function queueMicrotask(fn: Function): void;
1010
```
1111

1212
## [Entry points]({docs-version}/docs/usage#h-entry-points)
13-
```ts
13+
```plaintext
1414
core-js(-pure)/stable|actual|full/queue-microtask
1515
```
1616

17+
## [TypeScript type definitions]({docs-version}/docs/typescript-type-definitions)
18+
[`@core-js/types/web/queue-microtask`](https://github.com/zloirock/core-js/blob/v4-types/packages/core-js-types/src/base/web/queue-microtask.d.ts)
19+
1720
## Example
1821
```js
1922
queueMicrotask(() => console.log('called as microtask'));

docs/web/docs/features/web-standards/setimmediate.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ function clearImmediate(id: number): void;
1010
```
1111

1212
## [Entry points]({docs-version}/docs/usage#h-entry-points)
13-
```ts
13+
```plaintext
1414
core-js(-pure)/stable|actual|full/set-immediate
1515
core-js(-pure)/stable|actual|full/clear-immediate
1616
```
1717

18+
## [TypeScript type definitions]({docs-version}/docs/typescript-type-definitions)
19+
[`@core-js/types/web/efficient-script-yielding`](https://github.com/zloirock/core-js/blob/v4-types/packages/core-js-types/src/base/web/efficient-script-yielding.d.ts)
20+
1821
## Examples
1922
```js
2023
setImmediate((arg1, arg2) => {

docs/web/docs/features/web-standards/structured-clone.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ function structuredClone(value: Serializable, { transfer?: Sequence<Transferable
1010
```
1111

1212
## [Entry points]({docs-version}/docs/usage#h-entry-points)
13-
```ts
14-
core-js(-pure)/stable|actual|full/structured-clone
13+
[`@core-js/types/web/structured-clone`](https://github.com/zloirock/core-js/blob/v4-types/packages/core-js-types/src/base/web/structured-clone.d.ts)
14+
15+
## [TypeScript type definitions]({docs-version}/docs/typescript-type-definitions)
16+
```plaintext
17+
@core-js/types/web/structured-clone
1518
```
1619

1720
## Examples

docs/web/docs/features/web-standards/url-and-urlsearchparams.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ class URLSearchParams {
4646
```
4747

4848
## [Entry points]({docs-version}/docs/usage#h-entry-points)
49-
```ts
49+
```plaintext
5050
core-js/proposals/url
5151
core-js(-pure)/stable|actual|full/url
5252
core-js(-pure)/stable|actual|full/url/can-parse
5353
core-js/stable|actual|full/url/to-json
5454
core-js(-pure)/stable|actual|full/url-search-params
5555
```
5656

57+
## [TypeScript type definitions]({docs-version}/docs/typescript-type-definitions)
58+
[`@core-js/types/web/url-to-json`](https://github.com/zloirock/core-js/blob/v4-types/packages/core-js-types/src/base/web/url-to-json.d.ts)
59+
5760
## Examples
5861
```js
5962
URL.canParse('https://login:[email protected]:8080/?a=1&b=2&a=3&c=4#fragment'); // => true

docs/web/docs/menu.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"title": "Supported engines and compatibility data",
88
"url": "{docs-version}/docs/engines"
99
},
10+
{
11+
"title": "TypeScript type definitions",
12+
"url": "{docs-version}/docs/typescript-type-definitions"
13+
},
1014
{
1115
"title": "Features",
1216
"children": [
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# TypeScript Type Definitions
2+
3+
This package contains types for the global and pure versions of `core-js`.
4+
Although `core-js` is a JavaScript standard library polyfill, the built-in TypeScript types are often not sufficient.
5+
Additional types are needed for:
6+
- features that are already in JavaScript but not yet in TypeScript’s standard types;
7+
- proposals, including those already implemented in JavaScript engines;
8+
- explicit imports of features from the pure version.
9+
10+
It is shipped as a separate package because we cannot guarantee stable behavior with future TypeScript releases, including minor ones.
11+
12+
## Installation
13+
```sh
14+
npm install --save @core-js/[email protected]
15+
```
16+
17+
## Usage
18+
Add to your `tsconfig.json`:
19+
```json
20+
{
21+
"compilerOptions": {
22+
"types": [
23+
"@core-js/types"
24+
]
25+
}
26+
}
27+
```
28+
or import it directly in your files:
29+
```ts
30+
import '@core-js/types';
31+
```
32+
`@core-js/types` includes all types and entry points for the global version, but it is recommended to select only the subset you actually use.
33+
34+
### Usage of subsets
35+
There are four main subsets:
36+
- `@core-js/types/actual` - types for all actual features, including stable ECMAScript, web standards and Stage 3 ECMAScript proposals;
37+
- `@core-js/types/es` - types for stable ECMAScript features only;
38+
- `@core-js/types/stable` - types for stable ECMAScript and web standards features;
39+
- `@core-js/types/full` - types for all features, including proposals.
40+
41+
You can import them the same way as the main package. For example, to use only the stable subset, add this to your `tsconfig.json`:
42+
```json
43+
{
44+
"compilerOptions": {
45+
"types": [
46+
"@core-js/types/stable"
47+
]
48+
}
49+
}
50+
```
51+
or import it directly in your files:
52+
```ts
53+
import '@core-js/types/stable';
54+
```
55+
56+
### Usage of specific features
57+
If you need types only for specific features, you can import them like this:
58+
```json
59+
{
60+
"compilerOptions": {
61+
"types": [
62+
"@core-js/types/proposals/joint-iteration",
63+
"@core-js/types/web/structured-clone"
64+
]
65+
}
66+
}
67+
```
68+
or import them directly in your files:
69+
```ts
70+
import '@core-js/types/proposals/joint-iteration';
71+
import '@core-js/types/web/structured-clone';
72+
```
73+
You can find types for specific features on the corresponding pages in the [documentation](https://core-js.io/{docs-version}/docs/).
74+
75+
## Types for the pure version
76+
### Base usage
77+
Add this to your `tsconfig.json`:
78+
```json
79+
{
80+
"compilerOptions": {
81+
"types": [
82+
"@core-js/types/pure"
83+
]
84+
}
85+
}
86+
```
87+
Then, when you import from the pure entry points, the corresponding types are picked up automatically:
88+
```ts
89+
import $findLast from '@core-js/pure/full/array/find-last';
90+
91+
$findLast([1, 3, 4, 2], v => v > 2); // => 4
92+
```
93+
94+
### Namespace usage in the pure version
95+
If you need to use multiple methods from the same namespace, you can add `@core-js/types/pure` to `tsconfig.json` and import the entire namespace:
96+
```ts
97+
import $array from '@core-js/pure/full/array';
98+
99+
$array.findLast([1, 3, 4, 2], v => v > 2);
100+
$array.flatMap([1, 2, 3], x => [x, x * 2]);
101+
```
102+
(note that this is not recommended since tree shaking does not properly work in this case)
103+
104+
## DOM types
105+
Some of the global types for web standards work correctly only with the DOM lib.
106+
You need to add DOM types to the `lib` section of your `tsconfig.json` in addition to `@core-js/types`. For example:
107+
```json
108+
{
109+
"compilerOptions": {
110+
"types": ["@core-js/types"],
111+
"lib": ["esnext", "dom"]
112+
}
113+
}
114+
```
115+
In the pure version, you can use these types without adding the DOM lib.

website/src/js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import javascript from 'highlight.js/lib/languages/javascript';
44
import typescript from 'highlight.js/lib/languages/typescript';
55
import json from 'highlight.js/lib/languages/json';
66
import bash from 'highlight.js/lib/languages/bash';
7+
import plaintext from 'highlight.js/lib/languages/plaintext';
78
import RunButtonPlugin from './hljs-run.js';
89

910
hljs.registerLanguage('js', javascript);
1011
hljs.registerLanguage('ts', typescript);
1112
hljs.registerLanguage('json', json);
1213
hljs.registerLanguage('sh', bash);
14+
hljs.registerLanguage('plaintext', plaintext);
1315

1416
let initialized = false;
1517
function init() {

0 commit comments

Comments
 (0)