Skip to content

Commit 2e0a91c

Browse files
authored
Add typings instructions from snippet users and export Snippet type (#541)
1 parent a1fe64c commit 2e0a91c

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

.changeset/red-falcons-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Export AnalyticsSnippet type and add directions (for snippet users).

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,41 @@ export default defineComponent({
158158
</script>
159159
```
160160

161+
162+
163+
### For snippet users only: How to add typescript support
164+
165+
1. Install npm package `@segment/analytics-next`
166+
167+
2. Create `./typings/analytics.d.ts`
168+
```ts
169+
// ./typings/analytics.d.ts
170+
import type { AnalyticsSnippet } from "@segment/analytics-next";
171+
172+
declare global {
173+
interface Window {
174+
analytics: AnalyticsSnippet;
175+
}
176+
}
177+
178+
```
179+
3. Configure typescript to read from the custom `./typings` folder
180+
```jsonc
181+
// tsconfig.json
182+
{
183+
...
184+
"compilerOptions": {
185+
....
186+
"typeRoots": [
187+
"./node_modules/@types",
188+
"./typings"
189+
]
190+
}
191+
....
192+
}
193+
```
194+
195+
161196
# 🐒 Development
162197

163198
First, clone the repo and then startup our local dev environment:

packages/browser/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,41 @@ export default defineComponent({
158158
</script>
159159
```
160160

161+
162+
163+
### For snippet users only: How to add typescript support
164+
165+
1. Install npm package `@segment/analytics-next`
166+
167+
2. Create `./typings/analytics.d.ts`
168+
```ts
169+
// ./typings/analytics.d.ts
170+
import type { AnalyticsSnippet } from "@segment/analytics-next";
171+
172+
declare global {
173+
interface Window {
174+
analytics: AnalyticsSnippet;
175+
}
176+
}
177+
178+
```
179+
3. Configure typescript to read from the custom `./typings` folder
180+
```jsonc
181+
// tsconfig.json
182+
{
183+
...
184+
"compilerOptions": {
185+
....
186+
"typeRoots": [
187+
"./node_modules/@types",
188+
"./typings"
189+
]
190+
}
191+
....
192+
}
193+
```
194+
195+
161196
# 🐒 Development
162197

163198
First, clone the repo and then startup our local dev environment:

packages/browser/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export * from './core/context'
66
export * from './core/events'
77
export * from './core/plugin'
88
export * from './core/user'
9+
10+
export type { AnalyticsSnippet } from './browser/standalone-analytics'

0 commit comments

Comments
 (0)