-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Treat assignments to properties on functions as valid declarations #15868
Labels
Milestone
Comments
DanielRosenwasser
added
In Discussion
Not yet reached consensus
Suggestion
An idea for TypeScript
labels
May 16, 2017
My proposal at #16918 seems related to this issue; at least where the contextual type only has optional properties. |
RyanCavanaugh
added
Committed
The team has roadmapped this issue
and removed
In Discussion
Not yet reached consensus
labels
Oct 30, 2017
Approved for function declarations and consts initialized with a function expression; only applies when the declaration and the property write occur in the same containing function (or global) |
4 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Background
Something that newcomers run into as a result of migrating to TypeScript is that certain patterns don't work quite as easily anymore. For example, let's consider the following example:
This example doesn't work in TypeScript because
propTypes
isn't a valid property onFoo
's type.In the above case, there are two potential workarounds:
Find/declare a type that exists that is callable and has a valid property:
This option has the benefit of being able to statically catch incorrectly named declarations, but is somewhat unattractive because of the placement. It also doesn't work well if the original declaration was a function declaration like so:
Because it would need to be rewritten to a variable declaration to allow an annotation.
Rewrite the
const
declaration to a function declaration and use namespace merging:This option takes advantage of the
namespace
construct in TypeScript which performs value-merging; however, it is potentially worse becausefunction
declarations. This requires a rewrite from anyconst
declarations.Unless there's a new construct which creates a callable object (cc @bterlson), then I want to discuss the option of recognizing property assignments on functions. In other words, the original example would simply be something that TypeScript could understand.
Proposal
For an effective function declaration named
F
at the top-level scope, the first assignment to a propertyp
onF
(i.e.F.p
) is considered a declaration of a property namedp
on the type ofF
.An effective function declaration can be described by a FunctionDeclaration or a VariableDeclaration whose initializer is
Precedent
We already do this to some extent in
checkJs
mode because we simply have to. Not doing this would lead to an unacceptable experience there.Drawbacks
Alternative Ideas
Augmented variable declaration syntax
Allow namespaces to merge with
const
/let
/var
declarations.Find a way to cleanly coerce the type of a function declaration.
The text was updated successfully, but these errors were encountered: