简体   繁体   中英

TypeScript Type: String matching pattern

I am querying a system API (Linux' DBus API) that expects 3 string s:

  • service
  • path
  • name

So I could write the signature of the TypeScript methods that calls the interface like so:

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

But I know, service , path and name each follow specific string patterns:

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

(Regex not 100% correct, but here for comprehensibility)

I am wondering if it is possible to type guard the 3 parameters to match these patterns, like

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

You can read the comments and maybe find something useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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