Skip to content
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

Option to make this in functions inside object literals typed #12785

Closed
FranklinWhale opened this issue Dec 9, 2016 · 6 comments
Closed

Option to make this in functions inside object literals typed #12785

FranklinWhale opened this issue Dec 9, 2016 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@FranklinWhale
Copy link

FranklinWhale commented Dec 9, 2016

The lack of IntelliSense to this in functions inside object literals was reported in #7801 and #8110. #8382 attempted to fix that but it was reverted via #8389 due to #8191.

Adding this: this to functions in an interface, as mentioned in #9583, was a suggested way to workaround this issue. However, it is not convenient to define this: this for each method of an interface. Also, even when the object implements an interface, it may contain additional properties that are used in the interface methods but not exposed via the interface. Finally, like microsoft/vscode#298, there may be no interface at all.

Although typing this in functions inside object literals cannot be switched on by default, I think there should be a way to opt in. Could something be applied to an object literal to explicitly mark this in all methods of that object be the type of the surrounding object?

In the following examples, @ is a symbol that

  1. When it is placed immediately before the open bracket of an object literal, this in all methods of that object is of the type of the surrounding object.
  2. When it is located after this: in a method declaration inside an object literal, this in that method is of the type of the surrounding object.

Example 1--this being the surrounding object in all methods of an object literal:

let o = @{
	prop: 12,
	bar() {
		this.prop = 3; // `this` is inferred
	},
	foo() {
		this.bar(); // `this` is inferred
	}
};

Example 2--this being the surrounding object in a method of an object literal:

let o = {
	prop: 12,
	bar(this: @) {
		this.prop = 3; // `this` is inferred
	},
	foo() {
		this.bar(); // `this` is any here
	}
};

Example 3--this being the surrounding object in all methods of an object literal that implements other interfaces:

interface I {
	bar(); //`this: this` not required
	foo(); //`this: this` not required
}

let o1: I = @{
	prop: 12,
	bar() {
		this.prop = 3; // `this` is inferred
	},
	foo(this: any) {
		this.bar(); // `this` is any
	}
} as I;

let o2: I = <I>@{
	prop: 12,
	bar() {
		this.prop = 3; // `this` is inferred
	},
	foo(this: any) {
		this.bar(); // `this` is any
	}
};
@RyanCavanaugh RyanCavanaugh added Needs More Info The issue still hasn't been fully clarified Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed Needs More Info The issue still hasn't been fully clarified labels Dec 12, 2016
@RyanCavanaugh
Copy link
Member

var o: this already means something else, so you'd need to find some other syntactic place to put this.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 12, 2016

We have tried changing this behavior a while back and ran into some interesting scenarios that would not work, some relevant comments in #7801 (comment)

@FranklinWhale
Copy link
Author

@RyanCavanaugh: I have updated the examples to use @ instead of : this. I think that should work.

@mhegazy: I have read the comments and understand that this being any should be the best default. However, I think there should be a way to opt-in the behavior in #8382. What do you think about the use of the @ symbol?

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Dec 13, 2016
@gdh1995
Copy link
Contributor

gdh1995 commented Jan 14, 2017

I think it's badly needed!

It's too terrible to create an interface for every Object literal!

And I hate that I must declare a new member function whenever I want to add it to the origin literal.

As for POJO, well, this: Ember is much easier to write.

Anyone has a forked version of TypeScript 2 and the linter which support "auto-typed" this?

@ahejlsberg
Copy link
Member

See #14141.

@FranklinWhale
Copy link
Author

I think the suggested feature is no longer necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants