dw-innovation / dw-kid2-graph-frontend-prototype

dw-kid2-graph-frontend-prototype.vercel.app
MIT License
0 stars 0 forks source link

refactor types #84

Closed nikonikoniko closed 2 years ago

nikonikoniko commented 2 years ago

types should be refactored according to this structure:

// a service is a type that defines a service with arguments and return val, for now just a simple function
type Service<Args, Return> = (args: Args) => Return
// extract service Arg type
type Args<S> = S extends Service<infer X, infer Y> ? X : never
type Return<S> = S extends Service<infer X, infer Y> ? Y : never

// we can use these generic types to create a type:
type YoutubeService = Service<[boolean, string, string], number>
// Args<YoutubeService> ..... [string, string]

// now we can make a generic execution type:
type Execution<Service> = {
  id: string;
  of_data: Args<Service>;
  to_data: Return<Service>
}

// this causes our code to complain if we have the wrong types for the argument!!
const youtubeExecution: Execution<YoutubeService> = {
  id: "this id",
  of_data: [true, "string", "link2"],
  to_data: 4,
};

console.log(youtubeExecution)

/*
refactoring where types live
  @types/
    dbdoc
    data
    block
    execution
    service
    page
    context
    config
    components
    index exports all of them
*/

// execution/of_data: DataLink[]
// DataLink: { data_id: str, key: "" }
// DataLink: { ...data }

type Page = {
  "document/id": string;
  "document/type": "page";
  "page/title": string;
  "page/type": "admin" | "user";
  "page/content": string;
}
type Block = {
  "document/id":string;
  "document/type": "block";
  "block/type": 1 | 2 | 3 | 4;
  "block/title": string;
  "block/content": string;
}

// jsonschema
//  document_type: string
//  block/type?: number
//  page/type?: stringo

// anonymous objects

// object.block/type
// object["block/type"]
// object.block__type

// "block::type"

// const { "block/type": type, "document/id": id } = block;