Skip to content

Instantly share code, notes, and snippets.

@Tymek
Last active September 16, 2024 19:15
Show Gist options
  • Save Tymek/8fe2ad3495bb4a65d7bbb774febbb243 to your computer and use it in GitHub Desktop.
Save Tymek/8fe2ad3495bb4a65d7bbb774febbb243 to your computer and use it in GitHub Desktop.
TS type with indexed object and version property
// 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