From 7715ce8f4d74e730e3044a37a980c1666e7282a2 Mon Sep 17 00:00:00 2001 From: Gabby Skifstad Date: Tue, 9 Mar 2021 20:40:14 -0600 Subject: [PATCH] [@types/rosie]: allow 'option' arguments that serve purely as a tool to dynamically generate other attributes on the factory, and are not part of the resultant object's attributes. See https://github.com/rosiejs/rosie#programmatic-generation-of-attributes as an example of this use case (option 'numMatches' is just a dependency for the 'matches' attribute) --- types/rosie/index.d.ts | 1 + types/rosie/rosie-tests.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/types/rosie/index.d.ts b/types/rosie/index.d.ts index a6de6657ce005c..7ae92eb6156dfb 100644 --- a/types/rosie/index.d.ts +++ b/types/rosie/index.d.ts @@ -100,6 +100,7 @@ declare namespace rosie { attr(name: K, dependencies: [D1, D2], generatorFunction: (value1: T[D1], value2: T[D2]) => T[K]): IFactory; attr(name: K, dependencies: D[], generatorFunction: (value: T[D]) => T[K]): IFactory; attr(name: K, dependencies: D[], generatorFunction: any): IFactory; + attr(name: K, dependencies: string[], generatorFunction: (...dependencies: any[]) => T[K]): IFactory; /** * Convenience function for defining a set of attributes on this object as diff --git a/types/rosie/rosie-tests.ts b/types/rosie/rosie-tests.ts index ed53b76850d17d..32034ed39bd875 100644 --- a/types/rosie/rosie-tests.ts +++ b/types/rosie/rosie-tests.ts @@ -78,6 +78,20 @@ const person = Factory.build('Person'); let aString = ''; aString = person.firstName; +// It supports options not defined in the type definition +const personWithNicknameFactory = new Factory() + .attr('firstName', 'Frances') + .attr('lastName', 'Parker') + .option('nickname', null) + .attr('fullName', ['firstName', 'lastName', 'nickname'], (firstName, lastName, nickname) => { + if (nickname) { + return `${firstName} "${nickname}" ${lastName}`; + } + return `${firstName} ${lastName}`; + }); +// $ExpectType Person +const personWithNickname = personWithNicknameFactory.build({}, { nickname: 'Franny' }); + // Unregistered factories const unregisteredPersonFactory = new Factory();