chore(ts): migrate remaining hooks + editor/selection to TypeScript#53
Merged
Conversation
Last batch of the TS migration (#41 follow-up). Closes out the JS-side of `resources/scripts/`: the only `.js` files left are CI/test configs. ## Files - `editor/selection.ts` (from `.js`, 274 LOC) - Exports a discriminated `SelectionContext` union — `TextRangeSelection`, `WholeBlockSelection`, `MultiBlockSelection` — that consumers can `switch` on `mode`. `useEditorSelection` and `Composer.tsx`'s `SelectionChip` both pick this up automatically. - Local `WpSurface` narrow shape for the `wp.data` / `wp.richText` bits it touches, read via a one-call `wp()` helper to avoid colliding with `editor-bridge.ts`'s different `wp.data` overloads. - `hooks/use-editor-selection.ts` (from `.js`) - Returns `SelectionContext | null` from the shared selection module; discriminated narrowing in `sameSelection`. - `hooks/use-voice-input.ts` (from `.js`) - Local `SpeechRecognitionInstance` / `SpeechRecognitionEvent` / `SpeechRecognitionConstructor` for the Web Speech API surface we use, fetched via `getSpeechRecognition()` to dodge lib.dom.d.ts conflicts around the vendor-prefixed globals. - Exports `UseVoiceInputOptions` + `UseVoiceInput` interfaces. - `hooks/use-tts.ts` (from `.js`, 378 LOC) - Typed `useTtsEnabled` / `useVoiceMode` to return real `[boolean, (value: boolean) => void]` tuples — `MicButton.tsx` can now drop its manual narrowing cast (will follow up). - Local `ReadableMessage` shape for what `extractAssistantText` reads (string content OR a readonly array of `{type, text}` parts). ## Verification - `npm run typecheck` — clean - `npm run lint:js` — 0 warnings, 0 errors - `npm run build` — succeeds - `npm run test:unit` — 17/17 pass Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Last batch of the TS migration (#41 follow-up). Closes out the JS-side of
resources/scripts/: the only.jsfiles left are CI/test configs.Files
editor/selection.ts(from.js)SelectionContextunion (TextRangeSelection/WholeBlockSelection/MultiBlockSelection) consumers canswitchonmode. LocalWpSurfacenarrow shape forwp.data/wp.richTextread via a one-callwp()helper.hooks/use-editor-selection.ts(from.js)SelectionContext | null; discriminated narrowing insameSelection.hooks/use-voice-input.ts(from.js)SpeechRecognitionInstance/SpeechRecognitionEvent/SpeechRecognitionConstructorfor the Web Speech API surface. Reads the globals viagetSpeechRecognition()to dodge lib.dom.d.ts conflicts around the vendor-prefixed names. ExportsUseVoiceInputOptions+UseVoiceInputinterfaces.hooks/use-tts.ts(from.js)useTtsEnabled/useVoiceModenow return real[boolean, (value: boolean) => void]tuples —MicButton.tsxcan drop its manual narrowing cast in a follow-up. LocalReadableMessageshape forextractAssistantText.Design notes
Window.wpis intentionally NOT declared globally here.editor-bridge.tsalready declares the much widerwp.dataoverload set (dispatch + select for multiple stores);selection.tsneeds a strictly narrower surface. Each module reads via a local cast ((window as { wp?: ... }).wp) so the two shapes don't have to merge.SpeechRecognitionlikewise — lib.dom.d.ts ships it but typings vary across TS releases. Reading via a local cast keeps the hook compiling cleanly on every release.useTtsEnabled/useVoiceModereturn signatures now bind statically —MicButton.tsx'sas [boolean, (v: boolean) => void]cast at the destructure becomes redundant. Left for a follow-up to keep this PR pure-migration.Verification
npm run typecheck— cleannpm run lint:js— 0 warnings, 0 errorsnpm run build— succeedsnpm run test:unit— 17/17 passWraps up #41
After this lands, every
.js/.jsxfile inresources/scripts/(except CI/test configs) is TypeScript. The full migration arc:editor-bridge.tstypes/runtime.tsshared moduleuse-runtime-adapter.tscomponents/*.jsxeditor/selection.ts,use-editor-selection.ts,use-voice-input.ts,use-tts.ts🤖 Generated with Claude Code