how does one augment one of the ‘built-in’ types? eg Array?
In JS, I’d do something like
Array.prototype.shuffle = function () { ... };
what’s the equivalent in TypeScript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Types are ‘open ended’ in TypeScript, so you can just write:
And then the type is expanded to include the new function (and you can assign a function matching the signature to it).
Note however that extending the built-in types (those in lib.d.ts – such as Array) has an issue currently in the language service, as it caches those internally for perf reasons. Do the workaround I wrote-up at http://typescript.codeplex.com/workitem/4 to extend the built-in types without errors in the language service in VS.