Repository navigation
Description
Suggestion
π Search Terms
implicit function types
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
function literals should implicitly have a type - the same way class literals do.
π Motivating Example
Currently, to use dependency injection with functions, I have to explicitly add types for function literals, like so:
function sayHello(user: string) {
window.alert(`Hello, ${user}!`);
}
type sayHello = typeof sayHello; // π this
function start(sayHello: sayHello) {
// ...
}Intuitively, before you try that, you're going to try something this:
function sayHello(user: string) {
window.alert(`Hello, ${user}!`);
}
function start(sayHello: typeof sayHello) {
// ...
}That doesn't work.
Contrast function declarations with class declarations, which "just works":
class HelloSayer {
// ...
}
function start(sayHello: HelloSayer) {
// ...
}Why do class declarations get a type, and function declarations don't?
It's also not unconventional in JS to write "constructor" functions like this:
function HelloSayer(user: string) {
return {
sayHello() {
window.alert(`Hello, ${user}!`);
}
}
}
function start(sayHello: HelloSayer) {
// ...
}There's even been popular talk of "new keyword considered harmful" over the years.
In JavaScript, the result of a class declaration isn't very different from the result of a function declaration - both result in a function type value in the same scope. Both are declarations parsed during the first pass, meaning you can reference them recursively.
And functions do have a type in TypeScript: you can extract it with typeof, and you can import it from another module with import type, so why is this type "hidden" or inaccessible from type-hints? Why do classes have this special status?
I think this could be "fixed" retroactively without any breaking changes, if declaration merging could be applied, allowing existing code to explicitly declare an identical function-type with e.g. type HelloSayer = typeof HelloSayer, since these would almost definitely have identical types. (Or, at least, this would be a very minor breaking change, only affecting codebases where some types are currently inconsistent with the types of function literals with the same name.)
π» Use Cases
Dependency injection.
Activity
MartinJohns commentedon Jun 21, 2021
It works just fine, if you don't shadow the function name:
RyanCavanaugh commentedon Jun 21, 2021
It would be profoundly confusing (as well as actually a breaking change) to do this.
The implicit type declared by a class is not the type of the value, but the type of the instance shape. What equivalent is there for a function -- maybe its return type? The same goes for enum; the type it declares is not the same as its
typeof.The non-declaration of types here is fully intentional.
mindplay-dk commentedon Jun 22, 2021
I deliberately shadow names when injecting dependencies.
For one, inventing new names for no practical reason makes for really confusing code.
And for another, when you inject and shadow globals like
documentorwindow, you don't want somebody accidentally accessing the global rather than the injected instance of those objects by accident when making a change to the code.But it is the type of the value, e.g. something like:
{ new(): ... }with the actual instance shape being a structural subtype, the thing that is produced by the function when you applynewto it.But I'm not trying to debate which types are declared, but the fact that other symbol declarations have a named type - and functions for some reason don't.
It's kind of surprising if you look at the declaration types side by side - it really looks like an inconsistency in the language:
The error message even says:
'Foo' refers to a valueand suggeststypeof Foo- so the compiler actually knows what it is I'm trying to do, but for some reason is going to make me jump through hoops to do it.That's a matter of opinion.
Personally, I find it profoundly confusing when languages deviate from their own norms on fine points like this - inconsistency in programming languages is always confusing and frequently frustrating.
I don't think you've made a very convincing argument why function declarations shouldn't work like all other declarations. The outcome of a class declaration in JavaScript is literally the same thing as a function declaration - a local symbol with the same type:
I understand that it's a breaking change - I explained as much in my original post, but also explained why I think it's probably a very minor breaking change, and how this could probably be made backwards compatible with declaration merging.
If it helps you understand why this is important, I wrote an article about it. Software quality is very important to me - and I think that's the main reason anybody chooses TypeScript. If you care about software quality, you most likely apply IOC everywhere, and it's very likely this will lead you to functional programming patterns eventually.
This would just be more natural and easier to explain, if you could simply type-hint parameters against types. Getting an error-message for that is just a distraction, and
type f = typeof fis just noise that you have to explain.I wish you wouldn't dismiss the idea quite so promptly.
RyanCavanaugh commentedon Jun 22, 2021
This is not the first time this has been suggested, considered, and rejected, nor the first time we've thought about what kinds of declarations should introduce kinds of symbols.
mindplay-dk commentedon Jun 24, 2021
So you're saying others have felt the need for an improvement here.
What I'm suggesting may not be the answer, but it sounds like there's a real need here?
Or maybe I'm just reaching the point where I'm ready for a real functional programming language, heh π
mindplay-dk commentedon Jun 24, 2021
Maybe this could be an optional feature you turn on via
tsconfig?If this is not a feature you want at all, do you know if it's possible to implement this as a TS compiler plugin? (Since it's a feature that affects type-checking and doesn't change any syntax, an option or plugin shouldn't interfere with use of other compilers like Babel or ESBuild, so that approach might work.)
MartinJohns commentedon Jun 24, 2021
As per the documentation, language service plugins can't customize the type system.
fatcerberus commentedon Jun 24, 2021
typeof Classis the type ofClassas a runtime value--i.e. the class's constructor, with all its static members attached.Classby itself is the type of object you get by callingnewon it. They are completely different types, with no subtyping relationship. There is no equivalent to this dichotomy for regular functions.As for why regular functions don't have a named type, it's for the same reason
let x = 0;doesn't create a named type calledx. You have to usetypeof xto get its type. Same for functions.typeofgets the type of an identifier that's accessible at runtime. There is no separate runtime representation for the shape of a class instance (you have to actually call the constructor to get an instance), so TS creates a type that you can refer to when you need an instance.