Skip to content

Instantly share code, notes, and snippets.

@ClaireNeveu
Last active September 7, 2018 16:04
Show Gist options
  • Save ClaireNeveu/42749617cc184cd3ae74243a153a58b7 to your computer and use it in GitHub Desktop.
Save ClaireNeveu/42749617cc184cd3ae74243a153a58b7 to your computer and use it in GitHub Desktop.
// BEGIN Other type defs
type User = {
name: string
}
type Organization = {
name: string,
active: boolean
}
// END Other type defs
// BEGIN library code
class Id<A> {}
type Ref<A> = A | Id<A>
type _$MakeReferenceTable<O> = $ObjMap<O, <P>(P) => $Subtype<Ref<P>>>
type _$DowncastRefs<O> = $ObjMap<O, <P>($Subtype<Ref<P>>) => Id<P>>
type $RefAt<A: string, O: {}, D: {}> = $ElementType<_$DowncastRefs<$Diff<_$MakeReferenceTable<D>, O>> & O, A>
// END library code
type ThreadRefs = {|
user: User,
organization: Organization,
|}
class Thread<Refs: {}> {
length: number
user: $RefAt<'user', Refs, ThreadRefs>
organization: $RefAt<'organization', Refs, ThreadRefs>
constructor(args: {
length: number,
user: $RefAt<'user', Refs, ThreadRefs>,
organization: $RefAt<'organization', Refs, ThreadRefs>
}) {
this.length = args.length;
this.user = args.user;
this.organization = args.organization;
}
}
const unresolvedThread: Thread<{}> = (null: any)
const userId: Id<User> = unresolvedThread.user;
// const _userId: User = unresolvedThread.user; errors
const resolvedUserThread: Thread<{ user: User }> = (null: any)
const user: User = resolvedUserThread.user;
const orgId: Id<Organization> = resolvedUserThread.organization;
//const _user: Id<User> = resolvedUserThread.user; errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment