یافتن خطاهای متداول کدهای جاوا اسکریپتی با غنی سازی تنظیمات کامپایلر TypeScript
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۱۰/۰۴ ۱۴:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
defaultChecks() {
const author = { firstName: "Vahid", lastName: "N" };
console.log(author.lastname);
author.lastName.trimStart();
author.firstName.charCodeAt("1");
}
> npm install -g tslint typescript
{
"compilerOptions": {
"strictNullChecks": true
}
} getSessionItem(key: string): any {
const data = window.sessionStorage.getItem(key);
return JSON.parse(data);
} [ts] Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'. const data: string | null
getSessionItem(key: string): any {
const data = window.sessionStorage.getItem(key);
if (data) {
return JSON.parse(data);
} else {
return null;
}
} noImplicitReturns(a: number) {
if (a > 10) {
return a;
}
// No return in this branch
} {
"compilerOptions": {
"noImplicitReturns": true
}
} [ts] Not all code paths return a value.
{
"compilerOptions": {
"allowUnreachableCode": false
}
} allowUnreachableCode() {
if (false) {
console.log("Unreachable code");
}
const a = 1;
if (a > 0) {
return 10; // reachable code
}
return 0;
console.log("Unreachable code");
} unusedLocals() {
const a = "foo"; // Error: 'a' is declared but its value is never read
return "bar";
}
unusedParameters(n: number) {
n = 0; // Never read
} {
"compilerOptions": {
"noUnusedLocals": true,
"noUnusedParameters": true
}
} [ts] 'a' is declared but its value is never read. [ts] 'n' is declared but its value is never read.
excessPropertyForObjectLiterals() {
let x: { foo: number };
x = { foo: 1, baz: 2 }; // Error, excess property 'baz'
let y: { foo: number, bar?: number };
y = { foo: 1, baz: 2 }; // Error, excess property 'baz'
} {
"compilerOptions": {
"suppressExcessPropertyErrors": false
}
} [ts]
Type '{ foo: number; baz: number; }' is not assignable to type '{ foo: number; }'.
Object literal may only specify known properties, and 'baz' does not exist in type '{ foo: number; }'.
(property) baz: number fallthroughCasesInSwitchStatement(a: number) {
switch (a) {
case 0:
break;
case 1:
a += 1;
case 2:
a += 2;
break;
}
} {
"compilerOptions": {
"noFallthroughCasesInSwitch": true
}
} indexingObjectsLackingIndexSignatures() {
const x = { a: 0 };
x["a"] = 1; // ok
x["b"] = 1; // Error, type '{ a: number; }' has no index signature.
} {
"compilerOptions": {
"suppressImplicitAnyIndexErrors": false
}
} [ts] Element implicitly has an 'any' type because type '{ a: number; }' has no index signature. {
"compilerOptions": {
"noImplicitAny": true
}
} noImplicitAny(args) { // Error: Parameter 'args' implicitly has an 'any' type.
console.log(args);
} noImplicitAnyArgs(args: string[]) { // ok with the type information
console.log(args);
} /* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */