我有一个委托给validatorjs的方法,如下所示:
/**
* Checks if the string is a mobile phone number (locale is one of ['zh-CN', 'zh-TW', 'en-ZA', 'en-AU', 'en-HK',
* 'pt-PT', 'fr-FR', 'el-GR', 'en-GB', 'en-US', 'en-ZM', 'ru-RU', 'nb-NO', 'nn-NO', 'vi-VN', 'en-NZ']).
* If given value is not a string, then it returns false.
*/
export function isMobilePhone(value: string, locale: string): boolean {
return (
typeof value === "string" && vjsIsMobilePhone(value, locale)
);
}
VSCode 为locale
参数呈现以下错误:
[ts] 'string' 类型的参数不能分配给 'MobilePhoneLocale' 类型的参数。(参数)语言环境:字符串
该MobilePhoneLocale
类型来自@types/validator
。如果我分配MobilePhoneLocale
给locale
(而不是使用字符串),则该方法如下所示:
/**
* Checks if the string is a mobile phone number (locale is one of ['zh-CN', 'zh-TW', 'en-ZA', 'en-AU', 'en-HK',
* 'pt-PT', 'fr-FR', 'el-GR', 'en-GB', 'en-US', 'en-ZM', 'ru-RU', 'nb-NO', 'nn-NO', 'vi-VN', 'en-NZ']).
* If given value is not a string, then it returns false.
*/
export function isMobilePhone(value: string, locale: MobilePhoneLocale): boolean {
return (
typeof value === "string" && vjsIsMobilePhone(value, locale)
);
}
但是现在 ts 呈现此错误:
[ts] 找不到名称“MobilePhoneLocale”
locale
上述函数的类型应该如何实现?
还为此创建了一个@types/validator github 问题。
MobilePhoneLocale
在ValidatorJS
命名空间下声明,导入后即可访问validator
。您将其称为ValidatorJS.MobilePhoneLocale
. 这是一个使用示例ts-node
:
> import validator = require("validator")
{}
> function foo(l: ValidatorJS.MobilePhoneLocale): ValidatorJS.MobilePhoneLocale { return l; }
undefined
> foo("en-US")
'en-US'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句