Last active
September 16, 2024 19:15
-
-
Save Tymek/8fe2ad3495bb4a65d7bbb774febbb243 to your computer and use it in GitHub Desktop.
TS type with indexed object and version property
This file contains 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
// TypeScript type for an object with 1 property that is required but different then other properties | |
type Todo = { id: string; text: string; done: boolean } | |
type Table = Array<Record<string, unknown>> | |
type Schema = ( | |
| { | |
[key: string]: Table | |
} | |
| { | |
version: number | |
} | |
) & | |
Required<{ version: number }> | |
const doc: Schema = { | |
version: 1, // enforce that it is defined | |
todos: [] as Todo[], // other properties | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment