简体   繁体   English

TypeScript 类型:字符串匹配模式

[英]TypeScript Type: String matching pattern

I am querying a system API (Linux' DBus API) that expects 3 string s:我正在查询一个需要 3 个string的系统 API(Linux 的DBus API):

  • service
  • path
  • name

So I could write the signature of the TypeScript methods that calls the interface like so:所以我可以编写调用接口的 TypeScript 方法的签名,如下所示:

getInterface(service: string, path: string, name: string): DBusInterface;

But I know, service , path and name each follow specific string patterns:但我知道, servicepathname每个都遵循特定的字符串模式:

  • service = /^org\.bluez/ service = /^org\.bluez/
  • path = /(\/[a-zA-Z]*)+/ path = /(\/[a-zA-Z]*)+/
  • name = /([a-zA-Z]+\.)+/ name = /([a-zA-Z]+\.)+/

(Regex not 100% correct, but here for comprehensibility) (正则表达式不是 100% 正确,但这里是为了便于理解)

I am wondering if it is possible to type guard the 3 parameters to match these patterns, like我想知道是否可以键入保护 3 个参数以匹配这些模式,例如

type ServiceName: /^org\.bluez/

Currently as of 11/2019 this is not possible, there is an open suggestion for this for a while now that you can keep track of here https://github.com/Microsoft/TypeScript/issues/6579目前截至2019 年 11 月,这是不可能的,暂时有一个公开的建议,您可以在这里跟踪https://github.com/Microsoft/TypeScript/issues/6579

You can read the comments and maybe find something useful您可以阅读评论,也许会发现一些有用的东西

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

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