user4036692
user4036692

Reputation: 11

Is there a Typescript-Eslint rule on property access on optional property?

interface TypeA {
    optionalObj?: { prop: string }
}

const a: TypeA = {};

console.log(a.optionalObj.prop);

I want to find a Typescript-Eslint rule that will error on the line a.optionalObj.prop, because optionalObj can be undefined.

Is there such a rule?

Upvotes: 1

Views: 1130

Answers (1)

gatsbyz
gatsbyz

Reputation: 1075

This should be caught as a TS Error TS2532: Object is possibly 'undefined'.

If you're not seeing this error, fix your tsconfig.json. You should turn strictNullChecks on. Consider below flags.

   "strict": true,
    "alwaysStrict": true,

Upvotes: 1

Related Questions