File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -113,3 +113,23 @@ window.setTimeout(x.someMethod.bind(x), 100);
113
113
* Bad: In TypeScript, this currently has no type safety
114
114
* Bad: Only available in [ ECMAScript 5] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind ) or newer
115
115
* Bad: You have to type the instance name twice
116
+
117
+ ### Specify type of ` this ` in function signature
118
+ See details [ here] ( https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters ) .
119
+
120
+ ``` ts
121
+ interface SomeEvent {
122
+ cancelable: boolean ;
123
+ preventDefault(): void ;
124
+ }
125
+
126
+ function eventHandler(this : SomeEvent ) {
127
+ if (this .cancelable ) {
128
+ this .preventDefault ();
129
+ }
130
+ // ...
131
+ }
132
+ ```
133
+
134
+ * Good: The function has type information of the context it is supposed to run in, which is helpful in type checking and IDE completion
135
+ * Bad: The syntax of having ` this ` type declaration among function arguments might be confusing for developers at reading-time
You can’t perform that action at this time.
0 commit comments