Repository navigation
Ability to over power TS and ignore specific error by developer #9448
Description
Activity
Agreed. Longing for something like Java's
@SuppressWarnings, in particular for the case described here:The following:
const typeMetadataKey = Symbol('type'); function type(name: string): PropertyDescriptor { return Reflect.metadata(typeMetadataKey, name); }Produces the error:
Unable to resolve signature of class decorator when called as an expression..When used as below:
class Person { @type('string') firstName: string; }The decorator does work as expected and will compile but gives the error above.
If you have thoughts on how this might be resolved happy to dig into it if someone would like to point to the right direction.
Reacted by Artur and Bohdan BenetskyiJust cast it (cast isn't official term, but same concept)
const foo: string = 7 as any;
Is that what your looking for?
Reacted by Jay Wick, Steven, Sebastián González Montesinos, spion, Shellnut, Milad, Jason Kaczmarsky, Richard Scarrott, Victor Mejia, erndob and 71 moreReacted by Robert Shoemate, Ivan Chub, Yimi, acopalipsis, Joe Pea, A. Matías Quezada, Zhenya, Nikita Yu., Mikko Porkola, Kim Joar Bekkelund and 67 moreReacted by Sahin Erbay, LarsImNetz, Felipe Rodrigues, Kalinga Yaparathne, Jason Wang and FilipeReacted by Radoslav MladenovI just gave an example, not really a case (I know all about casting) , I do have other cases such as
super being called after first line of constructor and other issues...- will make transition into TS easier from JS + sometime you change a lib and get tons of errors and you just want to clean things up as you know as the developer for the reason...
this is an important feature
Reacted by Shaun Luttin, Andrei Neculau, Enterprise Software Architecture, Va Da, Fredrik Hjärner, Karl Tarvas and Bohdan BenetskyiSo something like
// tslint:disable?
Possibly even letting you turn on/off specific checkstscperforms?
eg:const FooBar: string = 'rozzzly'; // tslint:disable-line camelcaseReacted by Pablo Moleri, Steffen Reuther, Matt Michel, David Lauzon, Jakob Eriksen, monnef, Isaac Lyman, juno suárez, Christian Boehlke, WingGao and 25 morethat would be awesome...
Reacted by Enterprise Software ArchitectureI don't know... I think that might be out of the scope of
tsc. That's what linters are for.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. ;)
Reacted by Kfir ErezThe problem with "shutting it up" is you do not know what you get out of "it". so is
let a:string = 1anumberor astring?, what if there is another declaration ofa, does it merge or not? what if some one captured the type of this variable e.g.return {a} ;, should they be assignable to{ a : number }or{ a: string }, or both.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.
any, type assertions (casts), and ambient declaration.so for instance, if you have a library that has "invalid" definition, you can just remove it, and replace it with
declare module "blah" { export = any }. ordeclare var $: anyand you are good to go.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;
- Support shorthand ambient module declarations and wildcard chars in module names that allows for an easy way to disable a whole module or even a set of modules e.g.
declare module "@angular\*"; - Module resolution changes to allow you to override where a module definition is loaded from
- allowing for duplicate definitions with the same type accross .d.ts files
- new
--skipLibCheckto not type check any.d.tsfiles - support for
--typeRootsto allow overriding where declaration files are loaded from
Reacted by Meligy, Shlomi Borovitz, Chris Barr, BehindTheMath, Michael Stillwell, Bruno Lemos, Aaron Beall, Daniel Adams, salivarick, mcsky and 1 moreReacted by monnef, LinboLen and Ethan Reesor- Support shorthand ambient module declarations and wildcard chars in module names that allows for an easy way to disable a whole module or even a set of modules e.g.
- addedDiscussionIssues which may not have code impactIssues which may not have code impact
on Jun 30, 2016 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';
Reacted by maks187 and Mayur Jain (OMC-SF)Reacted by Herrington Darkholme, Felipe Almeida (queirozfcom), Miguel, Yauheni Prakopchyk, Lukas, DBot, Zev Spitz, Mathias Fischler, Charlie Guan, სანდრო and 2 moreReacted by Maksym Tymchyk and Bohdan Benetskyiok but again, the issue is not specifically on casting
Reacted by Robert Shoemate, Rajab Shakirov, Leo Caseiro, Kfir Erez, Rustam @SecondFry Gubaydullin, Maksim Luzik, John Hargrove, Mikko Porkola, Chahahou, Varun Munjeti and 18 more<any>gives you vanila javascript with 100% freedom from all annoying things of TypeScript, so what else do you need?Reacted by Ryan Cavanaugh, Arnaud Benhamdine, Chris Barr, Jacob, Charlie Guan and KazooReacted by monnef, Andrew Luhring, Shu Ding, Carl, Egor, mohemohe, Russell Dempsey, Fabian Lauer, Dylan Piercey, buratinodev and 29 morein 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:
ClassA.tsclass A { constructor() { this.init(); } protected init() { // does nothing by itself } }
ClassB.tsclass 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.
Reacted by DCoderLT, Antti Pihlaja, Shlomi Borovitz, Aks, Mathias Fischler, Molik Miah, Sara, Aaron Beall and Zev SpitzReacted by Karl Tarvas and Eric FerreiraReacted by Molik Miah, Cerber-Ursi and Bohdan Benetskyi131 remaining items
- removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Sep 19, 2017 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.
Reacted by Aluan Haddad and Peter KieltykaThis instruction works only per file, right? Is it possible to make it work over folder?
Reacted by Peter Kieltykanxpatterns commented
on Jan 10, 2018 More actionsWhy 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?
Reacted by B, Ben Smith, Stasi Berry, huishiyi, Jim, Chin Godawita, William Cantin, patheticcockroach, JoeOwner, Hedgehog Fog and 20 moreReacted by Kitson Kelly and Ives van HoorneReacted by B, huishiyi, Justin Vencel and barnonahill(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
// @ts-ignoreto your code(or even.// @ts-ignore <some code error>to ignore only the specified error)Tested it here with TypeScript 2.7.2 and it works!
Reacted by Vedran Mandić, David, leBekker, Elias-Leander Ahlers, Kir Belevich, Bartlomiej Bluszcz, Carlos Henrique, Ben Briggs, Lucas Bento, Nick Olinger and 8 moreReacted by Carlos Henrique and John CarlsonRyanCavanaugh commented
on Mar 20, 2018 MemberMore actionsRyan Cavanaugh (@RyanCavanaugh) you're right! I've updated my comment. Thanks!
Arrived here looking to suppress error TS2339.
document.getElementById('theme-admin').disabled = false; /* tslint:disable */ document.getElementById('theme-member').disabled = true; /* tslint:disable */- locked and limited conversation to collaborators
on Jul 31, 2018
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