This page is not yet available in Spanish. We are working on its translation. If you have any questions or feedback about our current translation project, feel free to reach out to us!
Metadata
ID:javascript-best-practices/valid-typeof
Language: JavaScript
Severity: Warning
Category: Error Prone
Description
Always compare typeof expressions against strings and make sure they are the correct values.
typeoffoo==='string';typeoffoo==='object';typeoffoo==='function';typeoffoo==='undefined';typeoffoo==='boolean';typeoffoo==='number';typeoffoo==='bigint';'string'===typeoffoo;'object'===typeoffoo;'function'===typeoffoo;'undefined'===typeoffoo;'boolean'===typeoffoo;'number'===typeoffoo;typeoffoo===typeofbar;typeoffoo===baz;typeoffoo!==someType;typeofbar!=someType;someType===typeofbar;someType==typeofbar;typeoffoo=='string';typeof(foo)==='string';typeof(foo)!=='string';typeof(foo)=='string';typeof(foo)!='string';varoddUse=typeoffoo+'thing';// since we don't have optios we are enforcing to always compare agaisnt strings
// function f(undefined) { typeof x === undefined };
typeoffoo==='number';typeoffoo==="number";varbaz=typeoffoo+'thing';typeoffoo===typeofbar;typeoffoo===`string`;`object`===typeoffoo;// not supported by this rule, we cannot pretend that somethingElse will complete 'string'
// typeof foo === `str${somethingElse}`;
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- javascript-best-practices # Rules to enforce JavaScript best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines