Skip to content

Instantly share code, notes, and snippets.

@ebeloded
Last active May 14, 2021 02:13
Show Gist options
  • Save ebeloded/820f470ca2c4bd39854ba838c9c57637 to your computer and use it in GitHub Desktop.
Save ebeloded/820f470ca2c4bd39854ba838c9c57637 to your computer and use it in GitHub Desktop.
document-and-collection.ts
const videoDoc = (id: string) => ({
get() {
return { id }
},
delete() {},
})
type QueryConstraint = number
const videosCollection = (...constraints: QueryConstraint[]) => ({
observe() {
return { constraints }
},
})
const videos = <
P extends Parameters<typeof videoDoc> | Parameters<typeof videosCollection>
>(
...v: P
): P extends Parameters<typeof videoDoc>
? ReturnType<typeof videoDoc>
: ReturnType<typeof videosCollection> => {
return typeof v[0] === 'string' ? videoDoc(v[0]) : videosCollection(...v)
}
type X = {
[id: string]: { get: () => string; update: () => string; delete: () => void }
(n: number): { observe: () => number }
}
let videos: X = 'a' as any
videos['vid'].get()
videos['vid'].update()
videos(123).observe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment