Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Type Run time type checking issue not working #27

Open
anu1097 opened this issue Mar 8, 2021 · 4 comments
Open

Custom Type Run time type checking issue not working #27

anu1097 opened this issue Mar 8, 2021 · 4 comments

Comments

@anu1097
Copy link

anu1097 commented Mar 8, 2021

I have a use case where I want to ensure my custom types are respected during run time as well. I would have written custom checks myself but Typescript's String Literal Types are not available during run time.

I am posting my code sample and generated code with ts-runtime library -

My Code -

type UploadMediaPayload = {
    abc: string
}

type analyticsEvent = {
  'Upload Media': UploadMediaPayload
}

function sampleFunction<Key extends keyof analyticsEvent>(evtName: Key, evtData: analyticsEvent[Key]){
    console.log(evtName, evtData);
}

const getData = () =>
  Promise.resolve({
      key: "key",
      value: "value"
  });
 
// usage (1)
getData()
  .then(data => {
    const {key, value} = data;
    sampleFunction(<keyof analyticsEvent>key, <UploadMediaPayload><unknown>value);
  });

getData();

sampleFunction("Upload Media", {abc: "abc"});
let key, value;
sampleFunction(key, value);

Generated Code

import t from "ts-runtime/lib";
const UploadMediaPayload = t.type("UploadMediaPayload", t.object(t.property("abc", t.string())));
const analyticsEvent = t.type("analyticsEvent", t.object(t.property("pload Medi", t.ref(UploadMediaPayload))));
function sampleFunction(evtName, evtData) {
    const Key = t.typeParameter("Key", t.any());
    let _evtNameType = t.flowInto(Key);
    let _evtDataType = t.any();
    t.param("evtName", _evtNameType).assert(evtName);
    t.param("evtData", _evtDataType).assert(evtData);
    console.log(evtName, evtData);
}
t.annotate(sampleFunction, t.function(fn => {
    const Key = fn.typeParameter("Key", t.any());
    return [t.param("evtName", t.flowInto(Key)), t.param("evtData", t.any()), t.return(t.any())];
}));
const getData = t.annotate(() => {
    return Promise.resolve({
        key: "key",
        value: "value"
    });
}, t.function(t.return(t.any())));
// usage (1)
getData()
    .then(t.annotate((data) => {
    const { key, value } = data;
    sampleFunction(key, value);
}, t.function(t.param("data", t.any()), t.return(t.any()))));
getData();
sampleFunction("Upload Media", { abc: "abc" });
let key, value;
sampleFunction(key, value);

analyticsEvent is not referred in the generated code. What do I have to make it work ?

@fabiandev
Copy link
Owner

fabiandev commented Mar 10, 2021

Types constructed with keyof are not implemented in ts-runtime at this point:

ts-runtime/src/factory.ts

Lines 135 to 144 in 963e9d8

case ts.SyntaxKind.MappedType:
// type Readonly<T> = {
// readonly [P in keyof T]: T[P];
// }
case ts.SyntaxKind.IndexedAccessType:
// function getProperty<T, K extends keyof T>(o: T, name: K): T[K] {
// return o[name]; // o[name] is of type T[K]
// }
case ts.SyntaxKind.TypeOperator:
// let a: keyof MyClass;

You should see warnings that reflections could not be found when generating the code. The runtime type will fall back to any in that case. See your example in the playground.

@anu1097
Copy link
Author

anu1097 commented Mar 10, 2021

Is there a feature statement written somewhere I can try to contribute.
Thanks for replying. I am looking for some solution for this problem checking out other libraries too.

@fabiandev
Copy link
Owner

There is no documentation around contributing, happy to answer questions though :)

However, please note that this package is a POC and not really intended to be used in production environments, mainly due to the impact on bundle size and execution time. Unfortunately I haven't had time to spend more time on the project to develop it further.

@anu1097
Copy link
Author

anu1097 commented Mar 10, 2021

Cool I'll check out your package to understand better. Then will ask further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants