You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-10Lines changed: 26 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,21 +9,37 @@ Type definition for [`Navigation API`](https://github.com/WICG/navigation-api)
9
9
```shell
10
10
$ npm i -D navigation-api-types
11
11
```
12
+
### Usage
12
13
13
-
###tsconfig.json
14
+
#### Make types visible in specific files
14
15
15
-
`navigation-api-types` needs to be added to `types` because it uses _ambient_ types that modify the global types.
16
+
Add a [TypesScript triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-)
17
+
as follows in any code-containing '.ts' file you want these types to be available in:
16
18
17
-
```json
18
-
{
19
-
"compilerOptions": {
20
-
"types": [
21
-
"./node_modules/navigation-api-types"
22
-
]
23
-
}
24
-
}
19
+
```typescript
20
+
// Add types for window.navigation for use in this file. See https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- for more info.
21
+
/// <referencetypes="navigation-api-types" />
22
+
23
+
console.log(window.navigation) // no type error!
24
+
```
25
+
26
+
#### Make types visible globally in all source files within a project
27
+
28
+
Create a `.d.ts` file anywhere in your project so that it is visible to TypeScript according to your `tsconfig.json` settings. For
29
+
example, it could be at `src/global.d.ts` or `src/navigation-api-types.d.ts`.
30
+
31
+
Add a [TypesScript triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) as follows:
32
+
33
+
```typescript
34
+
// Add types for window.navigation ambiently for implicit use in the entire project. See https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- for more info.
35
+
/// <referencetypes="navigation-api-types" />
25
36
```
26
37
38
+
This exposes the types *ambiently* so they are available without any `import` or `require` statements. TypeScript will simply know about them everywhere.
39
+
40
+
**Important**: do not add any `import` or `export` statements to this file, or it will stop working ambiently. Doing that
41
+
changes it in TypeScript's view from a "script" to a "module", and the rules about ambient types change in that case.
42
+
27
43
## License
28
44
29
45
This project is licensed under the [MIT License](https://github.com/lukewarlow/navigation-api-types/blob/master/LICENSE).
0 commit comments