|
| 1 | +--- |
| 2 | +title: Adopt in Existing TypeScript Apps |
| 3 | +slug: /getting-started/adopt-in-existing-typescript-apps |
| 4 | +description: The practical migration path for teams adopting Soundscript inside a working TypeScript application. |
| 5 | +--- |
| 6 | + |
| 7 | +Use this page when you are planning a real rollout in an existing TypeScript codebase. The point is |
| 8 | +not to “convert the repo.” The point is to start where stronger guarantees pay for themselves and |
| 9 | +expand only when the new boundary is earning its keep. |
| 10 | + |
| 11 | +## Start with modules that already act like trust boundaries |
| 12 | + |
| 13 | +Good first targets are usually: |
| 14 | + |
| 15 | +- request and queue handlers |
| 16 | +- decoders, parsers, and validation layers |
| 17 | +- auth and permission checks |
| 18 | +- package entrypoints or app service boundaries |
| 19 | +- code that already wraps risky legacy or third-party behavior |
| 20 | + |
| 21 | +Avoid broad utility folders or heavily shared framework glue as your first migration target. |
| 22 | + |
| 23 | +## A practical first week |
| 24 | + |
| 25 | +1. Install `soundscript` locally and run `soundscript init --mode existing`. |
| 26 | +2. Pick one boundary-heavy module and rename it to `.sts`. |
| 27 | +3. Mark every ordinary `.ts`, `.js`, or declaration-only import with `// #[interop]`. |
| 28 | +4. Remove unchecked escapes like `any`, `as`, and non-null assertions. |
| 29 | +5. Normalize foreign throws and make local conditions explicit. |
| 30 | +6. Add `soundscript check` to package scripts and CI. |
| 31 | + |
| 32 | +That is enough to learn the model without turning the adoption into a rewrite project. |
| 33 | + |
| 34 | +## Example migration boundary |
| 35 | + |
| 36 | +```ts title="src/payments/charge.sts" |
| 37 | +// #[interop] |
| 38 | +import {chargeCard} from '../legacy/payments.ts'; |
| 39 | +import {normalizeThrown} from 'sts:failures'; |
| 40 | + |
| 41 | +export async function runCharge(request: ChargeRequest): Promise<ChargeReceipt> { |
| 42 | + try { |
| 43 | + const receipt = await chargeCard(request); |
| 44 | + if (receipt.status !== 'ok') { |
| 45 | + throw new Error('Charge failed.'); |
| 46 | + } |
| 47 | + return receipt; |
| 48 | + } catch (error) { |
| 49 | + throw normalizeThrown(error); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +This is a good first migration because: |
| 55 | + |
| 56 | +- the boundary to legacy code is obvious |
| 57 | +- the failure path matters |
| 58 | +- the module already sits on an application seam |
| 59 | + |
| 60 | +## What usually changes first |
| 61 | + |
| 62 | +The first `.sts` modules rarely need advanced features. Most early fixes are just: |
| 63 | + |
| 64 | +- explicit `undefined` and `null` checks |
| 65 | +- replacing assertion-based typing with validation or narrowing |
| 66 | +- local `Error` discipline |
| 67 | +- visible interop boundaries |
| 68 | + |
| 69 | +That is exactly what you want. It means the adoption is paying off before you reach for machine |
| 70 | +numerics, macros, or advanced variance work. |
| 71 | + |
| 72 | +## What not to migrate first |
| 73 | + |
| 74 | +Defer these until the team is comfortable with the basics: |
| 75 | + |
| 76 | +- dense UI glue code |
| 77 | +- broad helper libraries with unclear ownership |
| 78 | +- reflection-heavy adapters |
| 79 | +- low-value leaf utilities with little boundary risk |
| 80 | +- framework internals where the team is still experimenting with architecture |
| 81 | + |
| 82 | +## Signs a module is a bad first target |
| 83 | + |
| 84 | +It is probably not a good first migration if: |
| 85 | + |
| 86 | +- most of the file is framework ceremony |
| 87 | +- the file is shared everywhere but trusted nowhere |
| 88 | +- it relies on metaprogramming or very loose ambient assumptions |
| 89 | +- the team cannot yet explain what the runtime boundary actually is |
| 90 | + |
| 91 | +## When this page matters |
| 92 | + |
| 93 | +Use this guide when you need to: |
| 94 | + |
| 95 | +- choose the first `.sts` modules |
| 96 | +- explain the rollout strategy to the team |
| 97 | +- decide what belongs in the first CI gate |
| 98 | +- keep the adoption scoped and realistic |
| 99 | + |
| 100 | +## See also |
| 101 | + |
| 102 | +- [Quick Start](./quick-start.md) |
| 103 | +- [Soundscript vs TypeScript](./soundscript-vs-typescript.md) |
| 104 | +- [Interop Boundaries](../guides/interop-boundaries.md) |
0 commit comments