David Sehnal 7 gadi atpakaļ
vecāks
revīzija
050edf26e1
1 mainītis faili ar 16 papildinājumiem un 3 dzēšanām
  1. 16 3
      docs/cif-schemas.md

+ 16 - 3
docs/cif-schemas.md

@@ -15,6 +15,12 @@ This is obviously not strongly typed + the "fields" dont know what type they are
 type FieldSchema<T> = { T: T /* remember the type */, createColumn: CIFField => Column<T> }
 type FieldSchema<T> = { T: T /* remember the type */, createColumn: CIFField => Column<T> }
 ```
 ```
 
 
+where column is just a function that for a given row returns a value of ``T``:
+
+```ts
+type Column<T> = (row: number) => T
+```
+
 Category schema is just an object whose properties are all instances of "field schemas", its "shape" has the type:
 Category schema is just an object whose properties are all instances of "field schemas", its "shape" has the type:
 
 
 ```ts
 ```ts
@@ -25,8 +31,8 @@ We can declare our first category "schema":
 
 
 ```ts
 ```ts
 const my_category = {
 const my_category = {
-  num_field: { T: 0 as number, createColumn: f => Column(f => f.getNumber) }
-  str_field: { T: '' as string, createColumn: f => Column(f => f.getString) }
+  num_field: { T: 0 as number, createColumn: f => f.getNumber }
+  str_field: { T: '' as string, createColumn: f => f.getString }
 }
 }
 ```
 ```
 
 
@@ -50,12 +56,19 @@ function toTypedCategory<Schema extends CategorySchema>(schema: Schema, category
         const field = category(key);
         const field = category(key);
         typedCategory[key] = field 
         typedCategory[key] = field 
             ? schema[key].createFolumn(field)
             ? schema[key].createFolumn(field)
-            : UndefinedColumn; // a column that always returns 0 or empty string depending on type
+            : UndefinedColumn(schema[key].T); // a column that always returns 0 or empty string depending on type
     }
     }
     return typedCategory;
     return typedCategory;
 }
 }
 ```
 ```
 
 
+This transforms the ''untyped'' ``Category`` to some typed category and gives us code-completion for CIF files:
+
+```ts
+const typed = toTypedCategory(my_category, ...);
+typed.n /* shows code completion for num_field */
+```
+
 And that's all there is to it. Extending the types to the "block" level is left as an exercise to the reader.
 And that's all there is to it. Extending the types to the "block" level is left as an exercise to the reader.
 
 
 ----------------
 ----------------