Code Monkey home page Code Monkey logo

Comments (3)

chriskrycho avatar chriskrycho commented on June 18, 2024 1

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 🏘️ on Thursday, so everything will be a bit spotty along the way, though.)

from ember-typings.

peabnuts123 avatar peabnuts123 commented on June 18, 2024

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.

peabnuts123 avatar peabnuts123 commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.