1

I follow this tutorial enter link description here to run the tests of a project written in angular and generated by jHipster but something seems to be wrong and some errors appear. This error appears when I run the tests with yarn test command:

ERROR: ....component.ts[31, 17]: Property 'callme'
is declared but its value is never read.
But this property is there for any reason. If I remove it the error is that the this property is not found. On both cases the tests cannot be run nor with Karma nor with Cucumber. I have installed the typescript library, tslint, karma plugins, cucumber, yarn. I also cleaned the npm cache deleted node_modules and did npm install. No result. The weird part is that there appear no errors in execution and run of the application, but only when I run teh tests. The developing team is going so far, meanwhile the tester (me) cannot even run a test. Has somebody experienced this? Please share with us the solution, cause I have been doing a 7 days research in the internet and tried almost everything.
Community
  • 1
  • 1
Tester
  • 449
  • 2
  • 10
  • 22
  • Have you declared it as a private property? If it's used in a template it needs to be a public property – Explosion Pills Apr 23 '18 at 16:29
  • No, they are private properties. – Tester Apr 23 '18 at 16:30
  • @ExplosionPills I'd say it's semantically debatable. Considering that private is effectively meaningless in TS I don't use it all, but if you're going to use it, and you consider a template to be logically part of its component, that seems reasonable. Tester, what makes you think you need to enforce the rule? – Aluan Haddad Apr 23 '18 at 19:25
  • @AluanHaddad Do not understad your question. But putting them public wasnt a solution. Why does it matter for typescript tho?? I don't get why are there errors that dont let me run the tests, but the application can be compiled very well, without any error. – Tester Apr 25 '18 at 11:13

1 Answers1

0

If you can, make the member public. If it is being accessed outside of the class this is the most honest way to write your code.

public callme: string;

In TypeScript, members are public by default, so you can just leave out the access modifier to the same effect.

If you have some reason to have a private member that is used by some external framework, you will need another solution. If the compiler can't see that a member is used, but you know there is some external use of it that the compiler couldn't possibly know about; you can take charge.

Here is one way you can do this:

// @ts-ignore: ignore not used error
private callme: string;

On the whole, you probably ought to avoid error suppression comments, but you may have found a case where it is necessary (it depends on what you are trying to achieve - my guessed concept is "I don't want my production code to access this member, but I am using a framework that works by convention to access it").

In the future, having a private member that is accessed non-privately may well break (if private access was ever enforced).

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • My question is why the error appears only when I hve to test the application, meanwhile when I build it or run no error appears. – Tester Apr 24 '18 at 07:12
  • If your test tool compiles the application first, it is possible it does so with different compiler options. There are a number of strict flags that highlight issues that are otherwise ignored. See https://www.typescriptlang.org/docs/handbook/compiler-options.html – Fenton Apr 24 '18 at 07:32
  • @tester Just don't use `private` properties. Their of dubious value in general but absolutely useless in components. It's also going to make using ES private properties a real pain for you in the future – Aluan Haddad Apr 25 '18 at 14:46
  • 1
    @fenton you're clearly knowledgeable, and your warning about error suppression is well taken, but I don't think this use case justifies it. Just remove the private modifier. Less code, no suppression, easier maintenance – Aluan Haddad Apr 25 '18 at 14:49
  • @AluanHaddad I have removed the private and made them public and them removed them at all and teh result is in this question. https://stackoverflow.com/questions/49980661/error-warning-the-no-unused-variable-rule-requires-type-infomation – Tester Apr 26 '18 at 08:10
  • @AluanHaddad I can see how this answer would be abused later on... I have tried to clarify the thinking and have noted that the "correct" answer is not to have the member be private (and added a note about what assumption was in my head when I wrote the answer). I hope this helps. – Fenton Apr 26 '18 at 10:29
  • @Fenton I gotta disaccept your answer cause the error doesnt appear anymore but no solution. I guess this only hides the error but doesnt help in my case to run the tests. – Tester Apr 26 '18 at 10:39