diff --git a/tests/cases/compiler/regexValidatedType.ts b/tests/cases/compiler/regexValidatedType.ts new file mode 100644 index 0000000000000..4d3ce3997cb51 --- /dev/null +++ b/tests/cases/compiler/regexValidatedType.ts @@ -0,0 +1,3 @@ +type CssColor = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i; +type Email = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i; +type Gmail = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@gmail\.com$/i; \ No newline at end of file diff --git a/tests/cases/compiler/regexValidatedTypeAssignment.ts b/tests/cases/compiler/regexValidatedTypeAssignment.ts new file mode 100644 index 0000000000000..f45a0d652caef --- /dev/null +++ b/tests/cases/compiler/regexValidatedTypeAssignment.ts @@ -0,0 +1,34 @@ +type Email = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i; +type Gmail = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@gmail\.com$/i; + +let email: Email; +let gmail: Gmail; +email = 'test@example.com'; +email = 'test@gmail.com'; +gmail = 'test@example.com'; +gmail = 'test@gmail.com'; +gmail = email; +email = gmail; + +gmail = email; +if (Gmail.test(email)) { + gmail = email; +} + +let someEmail = 'test@example.com'; +let someGmail = 'test@gmail.com'; +email = someEmail; +gmail = someGmail; +email = someEmail; +gmail = someGmail; +if (Email.test(someEmail)) { + email = someEmail; +} +if (Gmail.test(someGmail)) { + gmail = someGmail; +} + +let someEmailLiteral: 'test@example.com' = 'test@example.com'; +let someGmailLiteral: 'test@gmail.com' = 'test@gmail.com'; +email = someEmailLiteral; +gmail = someGmailLiteral; diff --git a/tests/cases/compiler/regexValidatedTypeIndex.ts b/tests/cases/compiler/regexValidatedTypeIndex.ts new file mode 100644 index 0000000000000..e5e296a00cece --- /dev/null +++ b/tests/cases/compiler/regexValidatedTypeIndex.ts @@ -0,0 +1,35 @@ +type Email = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i; +type Gmail = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@gmail\.com$/i; + +interface User { + usersProperty: number; +} +interface GmailUser { + gmailProperty: number; +} + +interface UsersCollection { + [email: Email]: User; + [gmail: Gmail]: GmailUser; +} + +let collection: UsersCollection; +let someEmail = 'test@example.com'; +let someGmail = 'test@gmail.com'; + +collection['test@example.com'].usersProperty; +collection['test@example.com'].gmailProperty; +collection['test@gmail.com'].usersProperty; +collection['test@gmail.com'].gmailProperty; + +collection[someEmail].usersProperty; +collection[someEmail].gmailProperty; +collection[someGmail].usersProperty; +collection[someGmail].gmailProperty; + +collection[someEmail].usersProperty; +collection[someEmail].gmailProperty; +collection[someGmail].usersProperty; +collection[someGmail].gmailProperty; +collection[ someGmail].usersProperty; +collection[ someGmail].gmailProperty;