Skip to content

Commit fec398a

Browse files
authored
🤖 Merge PR DefinitelyTyped#74613 Add initial typings for @hotwired/turbo-rails by @myabc
1 parent c3267a6 commit fec398a

5 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { cable, Turbo } from "@hotwired/turbo-rails";
2+
3+
// === Turbo re-export ===
4+
5+
// $ExpectType void
6+
Turbo.visit("https://example.com");
7+
8+
// $ExpectType void
9+
Turbo.visit("https://example.com", { action: "advance" });
10+
11+
// $ExpectType void
12+
Turbo.start();
13+
14+
const source: Turbo.StreamSource = {
15+
addEventListener(
16+
type: "message",
17+
listener: (event: MessageEvent) => void,
18+
options?: AddEventListenerOptions,
19+
): void {},
20+
removeEventListener(
21+
type: "message",
22+
listener: (event: MessageEvent) => void,
23+
options?: EventListenerOptions,
24+
): void {},
25+
};
26+
27+
// $ExpectType void
28+
Turbo.connectStreamSource(source);
29+
30+
// $ExpectType void
31+
Turbo.disconnectStreamSource(source);
32+
33+
// === cable namespace ===
34+
35+
async function testCable() {
36+
// $ExpectType Consumer
37+
const consumer = await cable.getConsumer();
38+
39+
// $ExpectType Consumer
40+
cable.setConsumer(consumer);
41+
42+
// $ExpectType Consumer
43+
const newConsumer = await cable.createConsumer();
44+
45+
// $ExpectType Subscription<BaseMixin>
46+
const subscription = await cable.subscribeTo("ChatChannel");
47+
48+
// $ExpectType Subscription<BaseMixin>
49+
const subscriptionWithParams = await cable.subscribeTo({ channel: "ChatChannel", room: "1" });
50+
51+
// subscribeTo with mixin
52+
const sub = await cable.subscribeTo("ChatChannel", {
53+
connected() {},
54+
received(data: any) {
55+
console.log(data);
56+
},
57+
});
58+
}
59+
60+
// @ts-expect-error
61+
cable.setConsumer("not a consumer");
62+
63+
// === turbo-cable-stream-source custom element ===
64+
65+
const streamSource = document.querySelector("turbo-cable-stream-source")!;
66+
67+
// $ExpectType TurboCableStreamSourceElement
68+
streamSource;
69+
70+
// $ExpectType Promise<void>
71+
streamSource.connectedCallback();
72+
73+
// $ExpectType void
74+
streamSource.disconnectedCallback();
75+
76+
// $ExpectType boolean
77+
streamSource.dispatchMessageEvent("<turbo-stream action='replace'></turbo-stream>");
78+
79+
// $ExpectType ChannelNameWithParams
80+
streamSource.channel;
81+
82+
// @ts-expect-error — channel is readonly
83+
streamSource.channel = { channel: "test" };
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as Turbo from "@hotwired/turbo";
2+
import { BaseMixin, ChannelNameWithParams, Consumer, Subscription } from "@rails/actioncable";
3+
4+
export { Turbo };
5+
6+
export namespace cable {
7+
function getConsumer(): Promise<Consumer>;
8+
function setConsumer(newConsumer: Consumer): Consumer;
9+
function createConsumer(): Promise<Consumer>;
10+
function subscribeTo<M extends BaseMixin = {}>(
11+
channel: string | ChannelNameWithParams,
12+
mixin?: M & ThisType<Subscription & M>,
13+
): Promise<Subscription & M>;
14+
}
15+
16+
declare class TurboCableStreamSourceElement extends HTMLElement {
17+
static readonly observedAttributes: string[];
18+
connectedCallback(): Promise<void>;
19+
disconnectedCallback(): void;
20+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
21+
dispatchMessageEvent(data: string): boolean;
22+
subscriptionConnected(): void;
23+
subscriptionDisconnected(): void;
24+
readonly channel: ChannelNameWithParams;
25+
subscription: Subscription | undefined;
26+
}
27+
28+
declare global {
29+
interface HTMLElementTagNameMap {
30+
"turbo-cable-stream-source": TurboCableStreamSourceElement;
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"private": true,
3+
"name": "@types/hotwired__turbo-rails",
4+
"version": "8.0.9999",
5+
"projects": [
6+
"https://github.com/hotwired/turbo-rails"
7+
],
8+
"dependencies": {
9+
"@types/hotwired__turbo": "*",
10+
"@types/rails__actioncable": "*"
11+
},
12+
"devDependencies": {
13+
"@types/hotwired__turbo-rails": "workspace:."
14+
},
15+
"owners": [
16+
{
17+
"name": "Alexander Brandon Coles",
18+
"githubUsername": "myabc"
19+
},
20+
{
21+
"name": "Gareth Jones",
22+
"githubUsername": "G-Rath"
23+
}
24+
]
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictFunctionTypes": true,
11+
"strictNullChecks": true,
12+
"types": [],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true
15+
},
16+
"files": [
17+
"index.d.ts",
18+
"hotwired__turbo-rails-tests.ts"
19+
]
20+
}

0 commit comments

Comments
 (0)