Last active
May 14, 2021 02:13
-
-
Save ebeloded/820f470ca2c4bd39854ba838c9c57637 to your computer and use it in GitHub Desktop.
document-and-collection.ts
This file contains hidden or 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
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