After reading this answer to the question How is jQuery's $ a function and an object?
, it got me thinking. How would you define this kind of type in typescript?
In regular JS this is completely valid:
var f = function() { alert('yo'); }
f.foo = "bar";
alert(f.foo); // alerts "bar"
f(); // alerts "yo"
However in typescript, f.foo
would throw an error, Property 'foo' does not exist on type '() => void'
.
While you could achieve a similar result using bracket notation:
var f = function() { alert('yo'); }
f['foo'] = "bar";
alert(f['foo']); // alerts "bar"
f(); // alerts "yo"
This would entirely bypass the type system, and there for the type safety, of typescript.
Is there a way of implementing this type of functionality without violating the type safety of typescript?