简体   繁体   English

使用 Faker 获取和设置以下所有数据的性别

[英]Using Faker to get and set gender for all following data

I'm using Faker with TypeScript and am struggling to figure out how to capture the returned gender from Faker in a way that I can then use in subsequent calls.我将Faker与 TypeScript 一起使用,并且正在努力弄清楚如何以一种我可以在后续调用中使用的方式从 Faker 捕获返回的性别。 This is what I'd like to do:这就是我想做的:

const gender = faker.name.gender(true);  // <-- returns a string
const firstName = faker.name.firstName(gender);  
const middleName = faker.name.middleName(gender);  

Unfortunately, Faker's faker.name.firstName(gender) takes a GenderType enum value, not a string, even though you can use it like faker.name.firstName('male') .不幸的是,Faker 的faker.name.firstName(gender)采用GenderType枚举值,而不是字符串,即使您可以像faker.name.firstName('male')一样使用它。 That works, but using the variable gender doesn't work.这行得通,但使用变量gender行不通。

Am I missing something here?我在这里错过了什么吗?

faker.name.firstName has the type (gender?: GenderType) => string , where type GenderType = 'female' | 'male' | 0 | 1; faker.name.firstName的类型为(gender?: GenderType) => string ,其中type GenderType = 'female' | 'male' | 0 | 1; type GenderType = 'female' | 'male' | 0 | 1; *. *。

However, faker.name.gender has the type (binary?: boolean = false) => string .然而, faker.name.gender的类型是(binary?: boolean = false) => string It can return quite a broad range of values:它可以返回相当广泛的值:

> faker.name.gender()
'T* woman'
> faker.name.gender()
'Intersex'
> faker.name.gender()
'Cis'

Unhelpfully, even if you set binary to true , what it returns is "Female" or "Male" , neither of which is actually a GenderType :毫无帮助的是,即使您将binary设置为true ,它返回的是"Female""Male" ,它们实际上都不是GenderType

> faker.name.gender(true)
'Male'
> faker.name.gender(true)
'Female'

although there is some normalisation, so this works in JavaScript (but not TypeScript):虽然有一些规范化,所以这在 JavaScript 中有效(但不是TypeScript):

> faker.name.firstName("Male")
'Ilene'

To make this work you'd have to add some type assertions , for example:要完成这项工作,您必须添加一些类型断言,例如:

import { faker, GenderType } from "@faker-js/faker";
const gender = faker.name.gender(true).toLowerCase() as GenderType;
                                                  // ^ we know this will be true
const name = faker.name.firstName(gender);

or, if you want to work with the capitalised version, using the intrinisic string manipulation types :或者,如果您想使用大写版本,请使用内在字符串操作类型

const gender = faker.name.gender(true) as "Female" | "Male";
                                    // ^ we know this will be true

// ...

const name = faker.name.firstName(gender.toLowerCase() as Lowercase<typeof gender>);
                                                    // ^ this is obviously true
                                                    //   (if ugly...)

These seem awkward enough that I've opened a bug to explore whether it can be fixed upstream.这些看起来很尴尬,我已经打开了一个错误来探索它是否可以在上游修复。

(Also it looks like the string case typing is being explored in TypeScript, so maybe the as Lowercase won't be needed at some point.) (另外,在 TypeScript 中似乎正在探索字符串大小写类型,因此在某些时候可能不需要as Lowercase Lowercase。)


* The numerical values are deprecated: *数值已弃用:

> faker.name.firstName(0)
[@faker-js/faker]: name.firstName(number) is deprecated since v6.1.0 and will be removed in v7.0.0. Please use 'female' or 'male' instead.
'Forrest'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM