Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document type helpers for oneOfs (#1023)
Add a section to the documentation that shows some TypeScript types that could be useful in working with union-style oneOfs. I've written these a few times in different projects so I could properly type helper functions that operate on generated oneOfs, and I figured it might be useful to other users of ts-proto. As an example, given the following: ```ts interface MusicPlayerRequest { command?: | { $case: "start"; start: StartCommand } | { $case: "stop"; stop: StopCommand } | { $case: "next"; list: NextCommand } | undefined; } ``` Then the helpers can be used like this: ```ts type MusicPlayerCommandNames = OneOfCases<MusicPlayerRequest['command']>; // = "start" | "stop" | "next" type MusicPlayerCommands = OneOfValues<MusicPlayerRequest['command']>; // = StartCommand | StopCommand | NextCommand type NextCommandByName = OneOfCase<MusicPlayerRequest['command'], 'next'> // = NextCommand function sendCommand<C extends MusicPlayerCommandNames>( commandName: MusicPlayerCommandNames, command: OneOfCase<MusicPlayerRequest['command'], C> ) { ... } // The `command` argument is automatically of type NextCommand because the `commandName` argument is "next" sendCommand("next", { skip: 1 }) ```
- Loading branch information