Asynchronous type guards and assertion signatures
## Search Terms async, asynchronous, promise, type guards, assertion signatures, asserts ## Suggestion TypeScript currently supports user-defined type guards and assertion signatures. ```typescript function isCustomType(value: unknown): value is CustomType { // ... } function assertIsCustomType(value: unknown): asserts value is CustomType { // ... } ``` I think it would be great if we could also define custom **asynchronous** type guards and assertions. ```typescript async function isCustomType(value: unknown): Promise<value is CustomType> { // ... } async function assertIsCustomType(value: unknown): Promise<asserts value is CustomType> { // ... } ``` ## Use Cases This feature would allow to check types and validate data asnychonously. Please look at the examples below. ## Examples Imagine the code like this: ```typescript interface User { name: string; } type Valid<T> = T & { readonly $valid: unique symbol; } async function createUser(user: User): Promise<void> { validateUser(user); await saveValidUserToDatabase(user); } function validateUser(user: User): asserts user is Valid<User> { if (user.name.length < 5) { throw new Error('User name must be at least 5 characters long'); } } async function saveValidUserToDatabase(user: Valid<User>): Promise<void> { // ... } ``` But sometimes, validation is done asynchronously, e.g. on server-side. Currently, you can achieve it this way: ```typescript async function createU
Posted by niko278 on Mar 30, 2020, 1:25 PM
Tags
Related issues
- Custom games stuck on "finding server" for all european servers
- Feature request: add ACP (Agent Client Protocol) stdio JSON-RPC mode
- Forza Horizon 6 (2483190)
- Turn off integrated browser for default
- Weekly updates are too frequent
- [BUG] Max 20x plan: 100% usage after 2 hours of light work (5 commits, no agents)
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
github (Sysadmin)
Custom games stuck on "finding server" for all european servers
Feature request: add ACP (Agent Client Protocol) stdio JSON-RPC mode
Forza Horizon 6 (2483190)
Turn off integrated browser for default
Weekly updates are too frequent
[BUG] Max 20x plan: 100% usage after 2 hours of light work (5 commits, no agents)
Add gemini-3.1-pro-preview
[TF2] Feature request: Bring Back Quickplay
Asynchronous type guards and assertion signatures
## Search Terms async, asynchronous, promise, type guards, assertion signatures, asserts ## Suggestion TypeScript currently supports user-defined type guards and assertion signatures. ```typescript function isCustomType(value: unknown): value is CustomType { // ... } function assertIsCustomType(value: unknown): asserts value is CustomType { // ... } ``` I think it would be great if we could also define custom **asynchronous** type guards and assertions. ```typescript async function isCustomType(value: unknown): Promise<value is CustomType> { // ... } async function assertIsCustomType(value: unknown): Promise<asserts value is CustomType> { // ... } ``` ## Use Cases This feature would allow to check types and validate data asnychonously. Please look at the examples below. ## Examples Imagine the code like this: ```typescript interface User { name: string; } type Valid<T> = T & { readonly $valid: unique symbol; } async function createUser(user: User): Promise<void> { validateUser(user); await saveValidUserToDatabase(user); } function validateUser(user: User): asserts user is Valid<User> { if (user.name.length < 5) { throw new Error('User name must be at least 5 characters long'); } } async function saveValidUserToDatabase(user: Valid<User>): Promise<void> { // ... } ``` But sometimes, validation is done asynchronously, e.g. on server-side. Currently, you can achieve it this way: ```typescript async function createU
Comments (0)
No comments yet. Start the discussion.