diff --git a/07-typestate-ts/main.ts b/07-typestate-ts/main.ts new file mode 100644 index 0000000..bf032e8 --- /dev/null +++ b/07-typestate-ts/main.ts @@ -0,0 +1,30 @@ +type State = "locked" | "unlocked"; + +class CredentialManager { + private readonly state?: S; + + unlock(this: CredentialManager<"locked">, master_key: string) { + return this as unknown as CredentialManager<"unlocked">; + } + + set(this: CredentialManager<"unlocked">, key: string, value: any) { + // ... + } + + get(this: CredentialManager<"unlocked">, key: string): string | undefined { + // ... + return undefined; + } + + lock(this: CredentialManager<"unlocked">): CredentialManager<"locked"> { + return this as unknown as CredentialManager<"locked">; + } +} + +function main2() { + let pw_locked = new CredentialManager<"locked">(); + let pw_unlocked = pw_locked.unlock("psst"); + pw_unlocked.set("foo", "bar"); + let val = pw_unlocked.get("foo"); + pw_locked = pw_unlocked.lock(); +} diff --git a/07-type-mapping/main.ts b/08-type-mapping/main.ts similarity index 96% rename from 07-type-mapping/main.ts rename to 08-type-mapping/main.ts index 90030b5..f5dccb4 100644 --- a/07-type-mapping/main.ts +++ b/08-type-mapping/main.ts @@ -30,7 +30,7 @@ class WebSocketClient { } } -function main_ws() { +function main1() { const client = new WebSocketClient(); client.send(OpCode.PING, {}); } diff --git a/08-type-programming/main.ts b/09-type-programming/main.ts similarity index 98% rename from 08-type-programming/main.ts rename to 09-type-programming/main.ts index 4ada33a..17a5415 100644 --- a/08-type-programming/main.ts +++ b/09-type-programming/main.ts @@ -23,7 +23,7 @@ function navigate

(path: P, params: ParseUrlParams

) { // ... } -function main() { +function main3() { navigate("/hello", {}); navigate("/hello/{name}", { name: "foo" }); navigate("/book/{author}/{id}", { author: "foo", id: "bar" }); diff --git a/Cargo.toml b/Cargo.toml index 222e172..935cde4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,3 @@ [workspace] members = ["01-problem", "02-runtime-check", "03-builder-pattern", "04-typestate"] +resolver = "3"