-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to over power TS and ignore specific error by developer #9448
Comments
Agreed. Longing for something like Java's
|
Just cast it (cast isn't official term, but same concept) const foo: string = 7 as any; Is that what your looking for? |
I just gave an example, not really a case (I know all about casting) , I do have other cases such as
this is an important feature |
So something like |
that would be awesome... |
I don't know... I think that might be out of the scope of |
there has to be able to "shut it up" :) |
I think there's a case to be argued for suppressing errors/warnings on "experimental" features, like decorators, where the API is a bit volatile and the errors may not always be accurate. You get a (very specific) version of this just using the tsconfig "experimentalDecorators" field, but it only suppresses one type of warning. To play my own devil's advocate, this could encourage new users of TypeScript to suppress warnings they do not understand instead of learning why the warning occurs. And with experimental features, everyone is sort of a new user - having the ability to suppress errors could make users complacent with bugs in new features, instead of opening issues. Ultimately, I still want my Syntastic output to be clean. Which means suppressing the error. Of course, that would be after I open the issue for a possible bug and try to learn more. ;) |
The problem with "shutting it up" is you do not know what you get out of "it". so is one fundamental thing, errors are all ignoble. errors do not block the generation of outputs, nor the tooling. There are different mechanisms to allow you to suppress checking of certain parts of your code, e.g. so for instance, if you have a library that has "invalid" definition, you can just remove it, and replace it with As i usually reply to these requests, i think your problem is not in suppressing the error. the real issue is you got an error that you do not find useful. suppress that does not solve the underlying problem it just covers it, and has ramifications of an inconsistent state with no warning. The right solution is to know what is the error you are getting? what library is it? and why the compiler is giving you an unuseful error... And for this we need to know more about your use case. We have done some work in TS 2.0 to resolve some of these underlying issues, for instance;
|
just use any, this is how "shut it up", merits of doing so (or actually a lack of thereof) is a different question let x: PieInTheSky = <any> 'cake is a lie'; |
ok but again, the issue is not specifically on casting |
|
in my case I call super not as fisrt line of constructor and need to quiet the error |
Instead of trying to force it to accept an antipattern, why not write try something like this:
class A {
constructor() {
this.init();
}
protected init() {
// does nothing by itself
}
}
class B extends A {
constructor() {
super();
console.log('rest of code from B\'s constructor');
}
protected init() {
console.log('this runs before the rest of code from B\'s constructor');
}
} This is what makes typescript so awesome, and also annoying. It forces you to write better code and it makes you a better developer. Converting a project is not fun; you might consider it to be a developer's "initiation" or perhaps, "trial by fire." 😆 But you learn a lot, and its totally worth it imho. |
And make your code incompatible with ES6... Which is exactly why the main purpose of TypeScript is to take 👣 🔫 out of your hands. If TypeScript is not interpreting something right, then it should be fixed versus "worked around". Now there are a few things where TypeScript is acting more like a linter and there is not yet a concept of "error" versus "warning". I can see suppressing warnings when they do come. Things like code after return and unused parameters should be warnings in my opinion, because they are syntactically correct (though stupid). |
here's another case where I would love to have this feature: interface Animal {
numberOfLegs: number;
// a gazillion more properties
}
class Dog implements Animal {
breed: string;
constructor(animal: Animal, breed: string) {
Object.assign(this, animal);
this.breed = breed;
}
} Right now there's an error from ts:
As you can see, the compiler is totally wrong, but I don't want to (and I shouldn't be forced to) copy all the properties from the interface just for the compiler's sake. |
@DethAriel Basically what you're asking for is a way to express post condition side effects in the type system. That's interesting but I have a feeling it would lead to some terribly convoluted code. |
@aluanhaddad Yup, I totally get that. But still, I do not see a non-ugly workaround for that except for copy-pasting the interface members, which is non-ideal at the very least. That's why I think that having the ability to shut up the compiler error output makes sense - we're all smart people here, and the compiler should trust us when we tell it to |
Just use an interface+class combo interface Animal {
numberOfLegs: number;
// a gazillion more properties
}
interface Dog extends Animal {
}
class Dog {
breed: string;
constructor(animal: Animal, breed: string) {
Object.assign(this, animal);
this.breed = breed;
}
} |
Thx, @mhegazy , that worked indeed |
What if the error can't be Im using the experimental bind syntax as discussed here #3508 and aside from not using it, I'm otherwise unable to get the compiler to ignore the error on each line before each |
this is really more than dismissing one warning. The parser does not support it, so the resulting tree is completely wrong, all of the compiler features from this point on will not work, so no type inference, no compatibility checks, no formatting, no completion, nothing. so you would be better off ignoring all errors, or just working in a .js file. |
I am currently converting a huge JS project into typescript and after doing the conversion when I run |
This is exactly my case as well, I convert an app built with pre-ES6 modules-as-properties design, so I have a HUGE app.namespace1.namespace2.something.views.view -like global object. I rewrite some part of it and I DO rely on the global app.* object and its different sub-elements in my code. All I get is a mass of "Cannot find namespace 'app'" warnings. I have refactored all my global dependencies to a globalProxy.ts, so this is the only place I get the warnings, but it would be AWESOME to add a //TS-NO-WARNINGS at the top of this file to clean up the console from the obvious messages... |
TS errors do not block the code generation. You could choose to ignore them, but what these are telling you is that the compiler can not assert the correctness of your code. |
@zeeshanjan82 why not use declare module '*'; |
Here's another use case for error suppression. The maintainers of the moment library forgot to add So we fixed the version of moment in place, but now we essentially can't use |
you could just add a local copy. say something as simple as: // ./overrides/moment.d.ts
declare module "moment"; // tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"baseUrl": ".",
"paths": {
"moment": ["overrides/moment.d.ts"] // override definition for moment
}
}
} now the compiler will be checking against your local copy of |
I am trying to manually set
But I get |
@amiraliakbari You can assert (window as any).console = {}; |
this worked for me to override/disable (window.console as any).___real_log = window.console.log;
window.console.log = function(args) {
if (Project.logging) return (window.console as any).___real_log(args);
return;
}; This was also much cleaner than putting |
As mentioned in #19109 we still don't have the ability to suppress a specific error. |
I think basic scenario outlined in this issue has been addressed. we can create a new issue to track global error suppression using error number. We have been reluctant to use error codes in such way because they lack expressiveness. |
Created #19139. |
This instruction works only per file, right? Is it possible to make it work over folder? |
The instruction is supposed to work for a single line at a time. If you're seeing a lot of compiler errors in your project, you might check that you should have less strict compiler options, such as leaving |
Why did you close this issue? The solution is still missing! Why do you have meaningless discussions for 2 years instead of integration a proper error-suppressing-possibility? |
(Adding this comment here as it might be useful for those who stumble upon this issue, as I did) I ran across #21602 and it might be the solution. Just add Tested it here with TypeScript 2.7.2 and it works! |
#21602 was not merged. You can't ignore only certain errors. |
@RyanCavanaugh you're right! I've updated my comment. Thanks! |
Arrived here looking to suppress error TS2339.
|
A developer should be able to add a comment above a ts error such as
and have the compiler not report that error...
there are certain scenarios the developer knows best and want to quiet down the error reports.
kind of like :any
regards
Sean
The text was updated successfully, but these errors were encountered: