Skip to content

Instantly share code, notes, and snippets.

@jamesplease jamesplease/0.notes.md
Last active Feb 8, 2019

Embed
What would you like to do?

TypeScript Notes

  • Use Pick to create ResourcePointers
  • Use a Record to create the final Store (maybe?)
  • Use the ResourceDefinition["idAttribute"] syntax to access the actual value of the ID

Ideas

  • Think about the API that I want to expose.
  • Don't worry about the internals: just focus on the API.
  • Then, think about ways to structure that internally to maximize type checking
  • Perhaps it makes sense for each create-resource.ts to manage its own updates and deletes? This would make it so that there are fewer dynamic types floating about.
type Breed = 'akita' | 'dachshund'
interface Dog {
name: string
}
const dogsByBreed: Record<Breed, Dog[]> = {
akita: [{ name: 'Walter' }, { name: 'Gracie' }],
dachshund: [{ name: 'Charlie' }],
}
// Here, the compiler will yield a type error:
// Property 'poodle' does not exist on type 'Record<Breed, Dog[]>'.
console.log('hi', dogsByBreed.akita);
// In this example, we use the passed-in definition to begin to create
// a valid Interface for this resource.
// type Book = ResourceSchema<'bookId'>;
// type ExternalIdAttribute<IdAttributeName extends string> = {
// readonly [key in IdAttributeName]: string | number;
// // readonly attributes: {}|undefined;
// };
// const x:ExternalIdAttribute<Book["idAttribute"]> = {
// bookId: 'wot',
// // id: 'hi'
// };
// type Thing = 'a';
// interface Test<AttrName extends Thing> {
// [AttrName]: string
// }
// type Test<AttrName> = {
// [AttrName]: string
// }
const books = createResource<Book, BookIdAttribute>('books', booksSchema);
const movies = createResource<Movie>('movies', moviesSchema);
const store = createResourceStore({
resources: [books, movies]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.