-
Notifications
You must be signed in to change notification settings - Fork 23
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
Use value from another field #41
Comments
It seems like the
|
Kind of an ugly interface, but I was able to do this via:
I think it would make more sense if there was an interface like:
|
Two years late, but I'm finally going through the backlog of issues a bit. I do like the API you propose, but I'm not sure how to solve the ordering issues introduced. Say for example you defined export const someFactory = Factory.Sync.makeFactory<ISomeType>({
id: uuidv4(), // I'm assuming this returns a Generator and specifying the same uuid for all
detailLink: 'TBD' // never used
}).withDerivation('detailLink', item => `/${item}`); Of you could declare detailLink: Sync.each(() => { throw "never reached" }), to really drive the point home. I'd also be fine with introducing |
Unfortunately the approach with *Derivation introduces some bad sideeffects: interface A {
prop1: string;
prop2: string;
prop3: string;
}
interface B {
a: A;
b: string;
}
const factory2 = Factory.Sync.makeFactory<B>(() => {
const value = "value";
return {
a: {} as A,
b: value,
};
}).withDerivation("a", (b) => ({
prop1: b.b,
prop2: b.b,
prop3: b.b,
}));
console.log(inspect(factory2.build({ b: "b" }), false, 10, true));
// Correctly prints: `{ a: { prop1: 'b', prop2: 'b', prop3: 'b' }, b: 'b' }`
console.log(
inspect(factory2.build({ b: "b", a: { prop1: "a" } }), false, 10, true)
);
// Incorrectly prints: `{ a: { func: [Function (anonymous)], prop1: 'a' }, b: 'b' }` The result is the same when using |
Further investigation shows that the const deriveFunc = <T extends { b: string }>(b: T) => ({
prop1: b.b,
prop2: b.b,
prop3: b.b,
});
const factory2 = Factory.Sync.makeFactory<B>(() => {
const value = "value";
return {
a: {} as A,
b: value,
};
}).withDerivation("a", deriveFunc);
console.log(
inspect(
{
"faultyResult.a": faultyResult.a,
//@ts-expect-error
"faultyResult.a.func": faultyResult.a.func,
//@ts-expect-error
"faultyResult.a.func.toString": faultyResult.a.func.toString(),
//@ts-expect-error
"faultyResult.a.func === deriveFunc": faultyResult.a.func === deriveFunc,
},
false,
10,
true
)
); prints: {
'faultyResult.a': { func: [Function: deriveFunc], prop1: 'a' },
'faultyResult.a.func': [Function: deriveFunc],
'faultyResult.a.func.toString': '(b) => ({\n prop1: b.b,\n prop2: b.b,\n prop3: b.b,\n})',
'faultyResult.a.func === deriveFunc': true
} |
Is it possible to use a field value, from another fields's value?
I.e.
The text was updated successfully, but these errors were encountered: