question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

targets and typescript

See original GitHub issue

Hi there!

Thanks for open sourcing stimulus. It’s been a real joy to use so far and I’m excited to really dig deep into it.

I noticed that the default example creates a typescript compiler error.

import { Controller } from "stimulus"

export default class extends Controller {
  static targets = [ "output" ]

  connect() {
    this.outputTarget.textContent = 'Hello, Stimulus!' //[ts] Property 'outputTarget' does not exist on type 'default'.
  }
}

The Stimulus documentation says:

When Stimulus loads your controller class, it looks for target name strings in a static array called targets. For each target name in the array, Stimulus adds three new properties to your controller. Here, our “source” target name becomes the following properties:

  • this.sourceTarget evaluates to the first source target in your controller’s scope. If there is no source target, accessing the property throws an error.
  • this.sourceTargets evaluates to an array of all source targets in the controller’s scope.
  • this.hasSourceTarget evaluates to true if there is a source target or false if not.

Is there anyway to let Typescript know about this.sourceTarget, this.sourceTargets, and this.hasSourceTarget? I thought this might be alleviate since the library is written in Typescript.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
sunnyrjunejacommented, Mar 12, 2018

Thank you @docstun! I see that someone has the same problem as me. I should have checked the forum too 😃. Since the maintainers are aware of the issue and there is a workaround, I’ll close this issue.

Workaround for future searchers:

import { Controller } from "stimulus"

export default class extends Controller {
  nameTarget: Element
  nameTargets: Element[] 
  hasNameTarget: boolean

  static targets = [ "name" ]

  // …
}
```
2reactions
sstephensoncommented, Nov 6, 2018

FYI:

If you use @sunnyrjuneja 's sulotion, you will need this config in tsconfig file, otherwise it will raise an error: xxxx has no initializer and is not definitely assigned in the constructor.

“strictPropertyInitialization”:false

You can use an exclamation point to bypass the strict initialization check:

class extends Controller {
  static targets = [ "name" ]

  readonly nameTarget!: Element
  readonly nameTargets!: Element[]
  readonly hasNameTargets!: boolean
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Option: target - TypeScript
The target setting changes which JS features are downleveled and which are left intact. For example, an arrow function () => this will...
Read more >
What is target in tsconfig.json for? - Stack Overflow
I am quite new to Typescript. What does Target ... target signifies which target of JavaScript should be emitted from the given TypeScript....
Read more >
Typescript - selecting proper target version - Dancing with CRM
Learn why you should choose proper target version in your tsconfig file and how it boost your code.
Read more >
Easy TypeScript: A Quick Guide to the target Option
The target option is configurable in your tsconfig.json or on the command-line via the --target flag. It refers to the JavaScript target that...
Read more >
TypeScript target | webhint documentation
typescript -config/target takes into account your webhint 's browserslist configuration and warns you if your target ( es3 , es2015 , etc.) is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found