Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

martinszeltins's avatar

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?

0 likes
1 reply

Please or to participate in this conversation.