Repository navigation
Typed 'this' in object literal methods - #14141
Merged
Merged
Conversation
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this PR we strongly type
thisin methods of object literals and provide a facility for controlling the type ofthisthrough contextual typing. The new behavior is only enabled in--noImplicitThismode.The type of the expression
thisin a method of an object literal is now determined as follows:thisparameter,thishas the type of that parameter.thisparameter,thishas the type of that parameter.--noImplicitThisis enabled and the containing object literal has a contextual type that includes aThisType<T>,thishas typeT.--noImplicitThisis enabled and the containing object literal has a contextual type that doesn't include aThisType<T>,thishas the contextual type.--noImplicitThisis enabledthishas the type of the containing object literal.thishas typeany.Some examples:
In a similar manner, when
--noImplicitThisis enabled and a function expression is assigned to a target of the formobj.xxxorobj[xxx], the contextual type forthisin the function expression isobj:In cases where an API produces a
thisvalue by transforming its arguments, a newThisType<T>marker interface can be used to contextually indicate the transformed type. Specifically, when the contextual type for an object literal isThisType<T>or an intersection includingThisType<T>, the type ofthiswithin methods of the object literal isT.In the example above, the
methodsobject in the argument tomakeObjecthas a contextual type that includesThisType<D & M>and therefore the type ofthisin methods within themethodsobject is{ x: number, y: number } & { moveBy(dx: number, dy: number): number }. Notice how the type of themethodsproperty simultaneously is an inference target and a source for thethistype in methods.The
ThisType<T>marker interface is simply an empty interface declared inlib.d.ts. Beyond being recognized in the contextual type of an object literal, the interface acts like any empty interface.Patterns similar to the above are used in several frameworks, including for example Vue and Ember. Using
ThisType<T>we can now more accurately describe those frameworks.Supercedes #8382. We revoked that PR because it always made the type of
thisin object literal methods be the type of the object literal. We now make that the default behavior, but allow the default to be overridden using aThisType<T>contextual type.