File tree 1 file changed +55
-0
lines changed
1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -39,3 +39,58 @@ class App {
39
39
}
40
40
}
41
41
```
42
+
43
+ ## I want to share some helper interfaces or aliases globally in my JSDoc project.
44
+
45
+ Turns out that you can share interfaces / type aliases globally in a project without copy pasting
46
+ or adding lots of ` @typedef {import(...)} ... ` statements.
47
+
48
+ The most important thing to do is to ` declare ` .
49
+
50
+ ``` ts
51
+ // typedefs.d.ts
52
+ declare type Result <T > = {
53
+ err: Error ,
54
+ data? : undefined
55
+ } | {
56
+ err? : undefined ,
57
+ data: T
58
+ }
59
+ ` ` `
60
+
61
+ Then in any javscript file you can use ` Result <null >` or ` Result <string []>` etc.
62
+
63
+ Do make sure that this file is included in your ` jsconfig .json `
64
+
65
+ ` ` ` json
66
+ {
67
+ " compilerOptions" : { ... },
68
+ " include" : [
69
+ " src/**/*.js" ,
70
+ " src/**/*.d.ts"
71
+ ]
72
+ }
73
+ ` ` `
74
+
75
+ I've set up my project to just include all ` .d .ts `
76
+
77
+ ### Can I use ` import ` with this ` typedefs .d .ts `
78
+
79
+ Yes you can, however adding [ ` import ` or ` export ` turns it into a module](https://stackoverflow.com/a/57040462/419970) and its no longer global.
80
+
81
+ You will have to write ` declare global { ... }` to be able to declare global interfaces/type aliases from a module.
82
+
83
+ ` ` `
84
+ // typedefs.d.ts
85
+ import * as events from ' events'
86
+
87
+ declare global {
88
+ type Result <T > = {
89
+ err: Error ,
90
+ data? : undefined
91
+ } | {
92
+ err? : undefined ,
93
+ data: T
94
+ }
95
+ }
96
+ ```
You can’t perform that action at this time.
0 commit comments