Linter Checks
A collection of extra checks, which somewhat cross the boundaries of compiler vs linter. You may prefer to use a tool like eslint over these options if you are looking for more in-depth rules.
No Fallthrough Cases In Switch - noFallthroughCasesInSwitch
Report errors for fallthrough cases in switch statements. Ensures that any non-empty case inside a switch statement includes either break
or return
. This means you won’t accidentally ship a case fallthrough bug.
consta : number = 6;Try
switch (a ) {case 0:Fallthrough case in switch.7029Fallthrough case in switch.console .log ("even");case 1:console .log ("odd");break;}
Default:
false
Released:
No Implicit Returns - noImplicitReturns
When enabled, TypeScript will check all code paths in a function to ensure they return a value.
Try
functionFunction lacks ending return statement and return type does not include 'undefined'.2366Function lacks ending return statement and return type does not include 'undefined'.lookupHeadphonesManufacturer (color : "blue" | "black"):string { if (color === "blue") {return "beats";} else {"bose";}}
Default:
false
Released:
noPropertyAccessFromIndexSignature - noPropertyAccessFromIndexSignature
This setting ensures consistency between accessing a field via the “dot” (obj.key
) syntax, and “indexed” (obj["key"]
) and the way which the property is declared in the type.
Without this flag, TypeScript will allow you to use the dot syntax to access fields which are not defined:
interfaceGameSettings {// Known up-front propertiesspeed : "fast" | "medium" | "slow";quality : "high" | "low";
// Assume anything unknown to the interface// is a string.[key : string]: string;}
constsettings =getSettings ();// ^ = (property) GameSettings.speed: "fast" | "medium" | "slow"settings .speed ;
// ^ = (property) GameSettings.quality: "high" | "low"settings .quality ;Try
// Unknown key accessors are allowed on// this object, and are `string`// ^ = stringsettings .username ;
Turning the flag on will raise an error because the unknown field uses dot syntax instead of indexed syntax.
constsettings =getSettings ();settings .speed ;settings .quality ;Try
// This would need to be settings["username"];Property 'username' comes from an index signature, so it must be accessed with ['username'].4111Property 'username' comes from an index signature, so it must be accessed with ['username'].// ^ = stringsettings .; username
The goal of this flag is to signal intent in your calling syntax about how certain you are this property exists.
Default:
false
Released:
noUncheckedIndexedAccess - noUncheckedIndexedAccess
TypeScript has a way to describe objects which have unknown keys but known values on an object, via index signatures.
interfaceEnvironmentVars {NAME : string;OS : string;
// Unknown properties are covered by this index signature.[propName : string]: string;}
declare constenv :EnvironmentVars ;
// Declared as existingconstsysName =env .NAME ;const// ^ = const os: stringos =env .OS ;Try
// Not declared, but because of the index// signature, then it is considered a stringconst// ^ = const nodeEnv: stringnodeEnv =env .NODE_ENV ;
Turning on noUncheckedIndexedAccess
will add undefined
to any un-declared field in the type.
declare constenv :EnvironmentVars ;
// Declared as existingconstsysName =env .NAME ;const// ^ = const os: stringos =env .OS ;Try
// Not declared, but because of the index// signature, then it is considered a stringconst// ^ = const nodeEnv: string | undefinednodeEnv =env .NODE_ENV ;
Released:
No Unused Locals - noUnusedLocals
Report errors on unused local variables.
Try
constcreateKeyboard = (modelID : number) => {const'defaultModelID' is declared but its value is never read.6133'defaultModelID' is declared but its value is never read.= 23; defaultModelID return {type : "keyboard",modelID };};
Default:
false
Released:
No Unused Parameters - noUnusedParameters
Report errors on unused parameters in functions.
Try
const'modelID' is declared but its value is never read.6133'modelID' is declared but its value is never read.createDefaultKeyboard = (: number) => { modelID constdefaultModelID = 23;return {type : "keyboard",modelID :defaultModelID };};
Default:
false
Released: