In other words: JavaScript is fast, JavaScript developers are slow.
But TS has a design constraint to be essentially a superset of JavaScript so it would be difficult to realize this goal.
This is polymorphic because union types are polymorphic.
let stringProcessor = (foo: string | number): string => {
//do stuff with foo
return "string" + foo
}
It's super easy to make non-optimized arrays and objects this way. type MyFoo = string | number //somewhere in the codebase
//you will get zero hints that this is a polymorphic function
let formatFoos = (foos: MyFoo[]) => foos.map((foo) => `${foo} deoptimized`))
Any use of optional object properties also immediately leads to polymorphism too. I doubt you can find ANY significant TS codebase where this isn't used pervasively. interface Foo {
foo: MyFoo //polymorphic
bar: string
baz?: number //this is polymorphic too
}
let doStuff = (obj: Foo): string {
//do stuff with obj
return "string"
}
doStuff({bar: "abc"})
doStuff({bar: "abc", baz: 123}) //we're now polymorphic
Even if we remove the `?`, it can still be polymorphic because the TS compiler doesn't reorder object properties when compiling. interface Foo {
bar: string //all basic types, so we're good?
baz: number //no more optional, so we're fine right?
}
let doStuff = (obj: Foo): string {
//do stuff with obj
return "string"
}
doStuff({baz: 123, bar: "abc"})
doStuff({bar: "abc", baz: 123}) //we changed order, so now we're now polymorphicDo you have a source for the above info? (both because I'm a little skeptical of some parts of it, but also because I'd love to learn more about this topic and I've had trouble finding concrete info in the past)
Viewed in those terms, it shouldn't be surprising that JS can be incredibly fast (faster than C in some cases).