RegularExpression for Specialized Signatures

Topics: Language Specification
Dec 6, 2013 at 6:59 AM
Hi, Guys.

Now TypeScript allow this code.
class HTMLElement {}
class HTMLSpanElement extends HTMLElement {

declare class Document {
    createElement(tag: "span"):HTMLSpanElement;
    createElement(tag: string):HTMLElement;

var document:Document = null;
document.createElement("span").iAmSpan(); // type safe!
this syntax very useful to keep interoperability existing JavaScript code.

I guess allow string literal and regular expression literal support to specialized signatures is more useful.
for example.
interface IWebApiResponse {}

interface IWebApiUsersResponse {
    users:{ name:string; }[];

interface IWebApiUserResponse {
    user:{ name:string; };

declare class Api {
    get(api:"/users", callback:(err:Error, response:IWebApiUsersResponse)=>void);
    get(api:/\/users\/[1-9][0-9]*/, callback:(err:Error, response:IWebApiUserResponse)=>void);
    get(api:string, callback:(err:Error, response:IWebApiResponse)=>void);

var api:Api = null;
api.get("/foobar", (err, response)=>{
api.get("/users", (err, response)=>{
    // typesafe!
    // response type is IWebApiUser"s"Response
api.get("/users/123", (err, response)=>{
    // typesafe! "/users/123" matches /users/[1-9][0-9]*
    // response type is IWebApiUserResponse
There is no significant impact on the syntactic.
There is the advantages that can be resolved at compile time all has not changed.
Overload resolution rules simplified is now available on 0.9.5.
isn't it?
I wonder if everyone like?
Dec 6, 2013 at 5:24 PM
We've talked about doing this and allowing regex in string-literal overloads/specializations. Like you point out, it's very powerful, and would be nice to have with some APIs. We're focused more on bugfixes for 1.0, so I suspect this is wouldn't make it into 1.0, but we can explore what adding this would mean after 1.0.

One reason we might not want to do this is how much overhead it would add to typechecking, so we'd need to do some experiments to see what the impact would be.
Dec 8, 2013 at 1:08 PM
Time spent to compile is certainly important.
I have been very excited also 1.x or later.
It is a very happy and get to think positively :)