11---
2- title : Get started
2+ title : Getting started
33pcx_content_type : get-started
44sidebar :
55 order : 2
66---
77
8- import { Render , TabItem , Tabs , PackageManagers , WranglerConfig } from " ~/components" ;
8+ import { Render , TabItem , Tabs , PackageManagers , WranglerConfig , TypeScriptExample } from " ~/components" ;
99
1010This guide will instruct you through:
1111
@@ -72,65 +72,38 @@ If you do not use JavaScript or TypeScript, you will need a [shim](https://devel
7272
7373Your ` MyDurableObject ` class will have a constructor with two parameters. The first parameter, ` ctx ` , passed to the class constructor contains state specific to the Durable Object, including methods for accessing storage. The second parameter, ` env ` , contains any bindings you have associated with the Worker when you uploaded it.
7474
75- <Tabs syncKey = " language" > <TabItem label = " JavaScript" icon = " seti:javascript" >
76-
77- ``` js
78- export class MyDurableObject extends DurableObject {
79- constructor (ctx , env ) {}
80- }
81- ```
82-
83- </TabItem > <TabItem label = " TypeScript" icon = " seti:typescript" >
84-
75+ <TypeScriptExample >
8576``` ts
86- export class MyDurableObject extends DurableObject {
77+ export class MyDurableObject extends DurableObject < Env > {
8778 constructor (ctx : DurableObjectState , env : Env ) {
8879 // Required, as we're extending the base class.
8980 super (ctx , env )
9081 }
9182}
9283```
93-
94- </TabItem > </Tabs >
84+ </TypeScriptExample >
9585
9686Workers communicate with a Durable Object using [ remote-procedure call] ( /workers/runtime-apis/rpc/#_top ) . Public methods on a Durable Object class are exposed as [ RPC methods] ( /durable-objects/best-practices/create-durable-object-stubs-and-send-requests/ ) to be called by another Worker.
9787
9888Your file should now look like:
9989
100- <Tabs syncKey = " language" > <TabItem label = " JavaScript" icon = " seti:javascript" >
101-
102- ``` js
103- export class MyDurableObject extends DurableObject {
104- constructor (ctx : DurableObjectState , env : Env ) {}
105-
106- async sayHello () {
107- let result = this .ctx .storage .sql
108- .exec (" SELECT 'Hello, World!' as greeting" )
109- .one ();
110- return result .greeting ;
111- }
112- }
113- ```
114-
115- </TabItem > <TabItem label = " TypeScript" icon = " seti:typescript" >
116-
90+ <TypeScriptExample >
11791``` ts
118- export class MyDurableObject extends DurableObject {
92+ export class MyDurableObject extends DurableObject < Env > {
11993 constructor (ctx : DurableObjectState , env : Env ) {
12094 // Required, as we're extending the base class.
12195 super (ctx , env )
12296 }
12397
124- async sayHello() {
98+ async sayHello(): Promise < string > {
12599 let result = this .ctx .storage .sql
126100 .exec (" SELECT 'Hello, World!' as greeting" )
127101 .one ();
128102 return result .greeting ;
129103 }
130104}
131105```
132-
133- </TabItem > </Tabs >
106+ </TypeScriptExample >
134107
135108In the code above, you have:
136109
@@ -151,39 +124,21 @@ A Worker is used to [access Durable Objects](/durable-objects/best-practices/cre
151124
152125To communicate with a Durable Object, the Worker's fetch handler should look like the following:
153126
154- <Tabs syncKey = " language" > <TabItem label = " JavaScript" icon = " seti:javascript" >
155-
156- ``` js
157- export default {
158- async fetch (request , env ) {
159- let id = env .MY_DURABLE_OBJECT .idFromName (new URL (request .url ).pathname );
160-
161- let stub = env .MY_DURABLE_OBJECT .get (id);
162-
163- let greeting = await stub .sayHello ();
164-
165- return new Response (greeting);
166- },
167- };
168- ```
169-
170- </TabItem > <TabItem label = " TypeScript" icon = " seti:typescript" >
171-
127+ <TypeScriptExample >
172128``` ts
173129export default {
174130 async fetch(request , env , ctx ): Promise <Response > {
175- let id = env .MY_DURABLE_OBJECT .idFromName (new URL (request .url ).pathname );
131+ const id: DurableObjectId = env .MY_DURABLE_OBJECT .idFromName (new URL (request .url ).pathname );
176132
177- let stub = env .MY_DURABLE_OBJECT .get (id );
133+ const stub = env .MY_DURABLE_OBJECT .get (id );
178134
179- let greeting = await stub .sayHello ();
135+ const greeting = await stub .sayHello ();
180136
181137 return new Response (greeting );
182138 },
183139} satisfies ExportedHandler <Env >;
184140```
185-
186- </TabItem > </Tabs >
141+ </TypeScriptExample >
187142
188143In the code above, you have:
189144
@@ -262,33 +217,35 @@ Preview your Durable Object Worker at `<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.de
262217
263218Your final code should look like this:
264219
220+ <TypeScriptExample >
265221``` ts title="index.ts"
266222import { DurableObject } from " cloudflare:workers" ;
267- export class MyDurableObject extends DurableObject {
223+ export class MyDurableObject extends DurableObject < Env > {
268224 constructor (ctx : DurableObjectState , env : Env ) {
269225 // Required, as we are extending the base class.
270226 super (ctx , env )
271227 }
272228
273- async sayHello() {
229+ async sayHello(): Promise < string > {
274230 let result = this .ctx .storage .sql
275231 .exec (" SELECT 'Hello, World!' as greeting" )
276232 .one ();
277233 return result .greeting ;
278234 }
279235}
280236export default {
281- async fetch(request , env , ctx ): Promise <Response > {
282- let id = env .MY_DURABLE_OBJECT .idFromName (new URL (request .url ).pathname );
237+ async fetch(request , env , ctx ): Promise <Response > {
238+ const id: DurableObjectId = env .MY_DURABLE_OBJECT .idFromName (new URL (request .url ).pathname );
283239
284- let stub = env .MY_DURABLE_OBJECT .get (id );
240+ const stub = env .MY_DURABLE_OBJECT .get (id );
285241
286- let greeting = await stub .sayHello ();
242+ const greeting = await stub .sayHello ();
287243
288244 return new Response (greeting );
289245 },
290246} satisfies ExportedHandler <Env >;
291247```
248+ </TypeScriptExample >
292249
293250By finishing this tutorial, you have:
294251
0 commit comments