-
Notifications
You must be signed in to change notification settings - Fork 85
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
Make this package work with generics #11
Comments
This would require exhaustive analysis of every possible context under which I don't think it is feasible to provide this feature. |
could you go into a bit more detail ? I'm noticing a pretty sweet use that would obviate the need to write factory methods per interface, for example: export function factoryInterface<T>(template = <Partial<T>>{}): T {
const keyCollection = keys<T>();
const data = <Partial<T>>{};
Object.keys(template)
.filter(key => keyCollection.includes(<keyof T>key))
.forEach(key => data[<keyof T>key] = template[<keyof T>key]);
keyCollection.forEach(key => {
if (!data.hasOwnProperty(key)) {
data[key] = undefined
}
});
return data as T;
} |
What JavaScript code do you think should be emitted as the result of the transformation of your function? |
The exact use case I have right now is the ability to create a property descriptor for an object so I can dynamically create a data grid by enumerating the model properties and emitting a column for each property. Basically something like:
This would be super useful to me right now. |
Ok ok I get that this wouldn't really work, but is there a work around of sorts? |
I know I haven't thought about it as much as I'm sure you have, and am probably missing something super obvious here but can you please provide a couple example use cases where the outputs conflict? Thinking out loud (and without [yet] reviewing the implementation), I wonder if there isn't a way to support some basic case(s) behind an experimental feature flag? |
@cheruvian For example, the JS output of the TS code in this comment would look like the following: export function factoryInterface(template = {}) {
const keyCollection = /* ??? */;
const data = {};
Object.keys(template)
.filter(key => keyCollection.includes(key))
.forEach(key => data[key] = template[key]);
keyCollection.forEach(key => {
if (!data.hasOwnProperty(key)) {
data[key] = undefined;
}
});
return data;
} What should the export function factoryInterface(keys, template = {}) {
const keyCollection = keys;
// ...
}
const obj1 = factoryInterface(['a', 'b'], { a: 'a' }); // original code: const obj1 = factoryInterface<{ a: string; b: number }>({ a: 'a' });
const obj2 = factoryInterface(['c', 'd'], { c: true }); // original code: const obj2 = factoryInterface<{ c: boolean; d: Date }>({ c: true }); However, I don't think this is feasible:
// TS input
function directCaller<T>() {
return factoryInterface<Omit<T, 'b' | 'c'>>();
}
const obj = directCaller<{ a: string; b: number, c: boolean, d: Date }>();
// JS output
function directCaller(arg) {
return factoryInterface(arg);
}
const obj = directCaller(['a', 'd']); // All type operations along the way need to be applied somehow |
I know in your README you mentioned that this doesn't work with generic types, i.e.
This functionality would be incredibly useful to me if it worked - and I would love to help contribute if you have any ideas for how to get this to work.
The text was updated successfully, but these errors were encountered: