Level 8
Jun 21, 2022
1
Level 14
Does TypeScript support RegEx types?
I would like to create some new string types in TypeScript that will show an error if it does not match a pattern. For example, I would like to create these types that will match a regex pattern.
type UnixFilesystemPath = '/something/something/something'
type WindowsFilesystemPath = 'X:\something\something'
type FtpPath = 'ftp://something/something'
let unixFile = 'test' // error
let unixFile = '/home/martin/script.sh' // ok
let unixFile = '/var/' // ok
Is this possible with TypeScript?
Please or to participate in this conversation.