Comments (3)
Hi @peabnuts123, thanks for the detailed writeup!
These are, sadly, well-known issues to me personally, and I'm working on a blog post (probably live tomorrow morning) which walks through the solution (below) for them in some detail. Also note that, thankfully, a lot of this will go away when the ES5 Getters RFC lands (probably Ember 3.1 or 3.2)
In the meantime, the workaround is to explicitly annotate the this
type for the methods and especially for computed property functions:
import Controller from '@ember/controller';
import { get, computed } from '@ember/object';
export default class ApplicationController extends Controller {
value: string;
// `this: ApplicationController` is what you need to make this work as expected.
init(this: ApplicationController) {
console.log(get(this, 'value'));
}
}
Things will generally not work especially well in the old-style .extends()
invocations, because there's no named type to give to the this
in that case.
Today's workaround in that case (which loses you type safety but makes things type-check) is to do (this as any).get(...)
there. Once this DefinitelyTyped PR merges and is liveβprobably the next day or twoβyou'll be able to do that with get(this as any, ...)
as well. And, since you're on the latest here and with TypeScript, you can also use // @ts-ignore
comments where necessary. (It's really unfortunate that all of these escape hatches remain necessary, but they should be getting much better in the next three or four months.)
This will be in the README as well as in a long example blog post sometime in the week. (I'm moving into a new house
from ember-typings.
Oh and FYI my packages@versions are as follows:
βββ @types/[email protected]
βββ @types/[email protected]
βββ @types/[email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
from ember-typings.
Thank for the quick response there Chris! Looking forward to that Getters RFC. The best solution I've found so far is to do something like
const self = this as ApplicationController
let value = get(self, value);
in functions, but it's definitely bringing back old jQuery memories. I will try out your this: ApplicationController
recommendation. Good to hear things are always on the move, though :)
from ember-typings.
Related Issues (17)
- Typings for qunit
- [DRAFT!!!] QUEST: Let's type the Ember.js ecosystem! HOT 10
- TS errors when extending object literal attempts to define/override init HOT 4
- `this` context lost in computed getters HOT 4
- Issues with current Route definitions HOT 4
- No type checks in other files when exporting `typeof MyModel.prototype` without another `typeof`. HOT 1
- Missing re-export for Rsvp
- Ember typing issues to fix HOT 3
- Interface 'NativeArray<T>' incorrectly extends interface 'MutableArray<T>' HOT 5
- cannot find module '@ember/error' HOT 3
- Using ES classes with Route, model, and async HOT 2
- [ember-data]: Type is referenced directly or indirectly in the fulfillment callback of its own 'then' method HOT 5
- Wrong typing for `this.addObserver` in a component HOT 2
- htmlSafe() has no return type but is SafeString HOT 3
- Mark as deprecated? HOT 1
- Typings for ember prototype extensions HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-typings.