basarat/typescript-collections

Could use casting class

Closed this issue · 1 comments

Typescript really in need of a casting mechanism, not sure if you're interested Basarat but everything you make is awesome so thought you might be interested! (http://stackoverflow.com/questions/31620809/how-to-create-trycast-in-typescript-net-equivalent)

so thought you might be interested

Thanks 🌹

As meniontioned by both the answers specialized function signatures are the way to go for now:

function tryCast(obj: number, type: "number"): number;
function tryCast(obj: string, type: "string"): string;
function tryCast(obj: boolean, type: "boolean"): boolean;
function tryCast(obj: IFoo, type: "IFoo"): IFoo;
function tryCast(obj: any, type: string): any;
function tryCast(obj: any, type: string): any 

The knowledge of <T> is lost at runtime. I've been thinking about some extensions to the language that would offer the generated .js the necessary information. Don't have any concrete proposal though.