{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6579","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6579/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6579/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6579/events","html_url":"https://github.com/Microsoft/TypeScript/issues/6579","id":128264906,"number":6579,"title":"Suggestion: Regex-validated string type","user":{"login":"tenry92","id":11708191,"avatar_url":"https://avatars.githubusercontent.com/u/11708191?v=3","gravatar_id":"","url":"https://api.github.com/users/tenry92","html_url":"https://github.com/tenry92","followers_url":"https://api.github.com/users/tenry92/followers","following_url":"https://api.github.com/users/tenry92/following{/other_user}","gists_url":"https://api.github.com/users/tenry92/gists{/gist_id}","starred_url":"https://api.github.com/users/tenry92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tenry92/subscriptions","organizations_url":"https://api.github.com/users/tenry92/orgs","repos_url":"https://api.github.com/users/tenry92/repos","events_url":"https://api.github.com/users/tenry92/events{/privacy}","received_events_url":"https://api.github.com/users/tenry92/received_events","type":"User","site_admin":false},"labels":[{"id":306536864,"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Domain:%20Literal%20Types","name":"Domain: Literal Types","color":"FB3A4B","default":false},{"id":114201833,"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Needs%20Proposal","name":"Needs Proposal","color":"556b75","default":false},{"id":113971155,"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":17,"created_at":"2016-01-22T22:50:35Z","updated_at":"2016-12-01T12:53:45Z","closed_at":null,"body":"There are cases, where a property can not just be any string (or a set of strings), but needs to match a pattern.\n\n``` typescript\nlet fontStyle: 'normal' | 'italic' = 'normal'; // already available in master\nlet fontColor: /^#([0-9a-f]{3}|[0-9a-f]{6})$/i = '#000'; // my suggestion\n```\n\nIt's common practice in JavaScript to store color values in css notation, such as in the css style reflection of DOM nodes or various 3rd party libraries.\n\nWhat do you think?\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/264166471","html_url":"https://github.com/Microsoft/TypeScript/issues/6579#issuecomment-264166471","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6579","id":264166471,"user":{"login":"Igmat","id":15427508,"avatar_url":"https://avatars.githubusercontent.com/u/15427508?v=3","gravatar_id":"","url":"https://api.github.com/users/Igmat","html_url":"https://github.com/Igmat","followers_url":"https://api.github.com/users/Igmat/followers","following_url":"https://api.github.com/users/Igmat/following{/other_user}","gists_url":"https://api.github.com/users/Igmat/gists{/gist_id}","starred_url":"https://api.github.com/users/Igmat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Igmat/subscriptions","organizations_url":"https://api.github.com/users/Igmat/orgs","repos_url":"https://api.github.com/users/Igmat/repos","events_url":"https://api.github.com/users/Igmat/events{/privacy}","received_events_url":"https://api.github.com/users/Igmat/received_events","type":"User","site_admin":false},"created_at":"2016-12-01T12:53:44Z","updated_at":"2016-12-01T12:53:44Z","body":"@alexanderbird, I don't suggest making this code valid or add some hidden checks in both runtime and compile time.\r\n```TypeScript\r\nfunction foo(bar: number) {\r\n    let baz: /prefix:\\d+/ = 'prefix:' + number;\r\n}\r\n```\r\nThis code have to throw error due to my proposal. But this:\r\n```TypeScript\r\nfunction foo(bar: number) {\r\n    let baz: /prefix:\\d+/ = ('prefix:' + number) as /prefix:\\d+/;\r\n}\r\n```\r\nor this:\r\n```TypeScript\r\nfunction foo(bar: number) {\r\n    let baz: /prefix:\\d+/;\r\n    let possibleBaz: string = 'prefix:' + number;\r\n    if (/prefix:\\d+/.test(possibleBaz)) {\r\n        baz = possibleBaz;\r\n    }\r\n}\r\n```\r\nwould be correct, and even have no impact to emitted code.\r\n\r\nAnd as I showed in previous comment, literals would be definitely not enough even for common use cases, because we often have to work with stings from user input or other sources. Without implementing of this emit impact, users would have to work with this type in next way:\r\n```TypeScript\r\ntype Email = /.../;\r\nconst Email = /.../;\r\nlet email: Email;\r\nlet userInput: string;\r\n// somehow get user input\r\nif (Email.test(userInput)) {\r\n    email = <Email>userInput;\r\n} else {\r\n    console.log('User provided invalid email. Showing validation error');\r\n    // Some code for validation error\r\n}\r\n```\r\nor for intersections:\r\n```TypeScript\r\nexport type Email = /email-regex/;\r\nexport const Email = /email-regex/;\r\nexport type Gmail = Email & /gmail-regex/;\r\nexport const Gmail = {\r\n    test: (input: string) => Email.test(input) && /gmail-regex/.test(input)\r\n};\r\nlet gmail: Gmail;\r\nlet userInput: string;\r\n// somehow get user input\r\nif (Gmail.test(userInput)) {\r\n    gmail = <Gmail>userInput;\r\n} else {\r\n    console.log('User provided invalid gmail. Showing validation error');\r\n    // Some code for validation error\r\n}\r\n```\r\nI don't think that forcing users to duplicate code and to use explicit cast, when it could be easily handled by compiler isn't a good way to go. Emit impact is really very small and predictable, I'm sure that it won't surprise users or lead to some feature misunderstood or hard to locate bugs, while implementing this feature without emit changes definitely WILL."}}
{"actor":{"display_login":"Igmat"}}