Home > Blockchain >  Nullable return type not working in Typescript
Nullable return type not working in Typescript

Time:10-07

Given this code

export interface ICollectionService {
  get(id: string): Promise<Collection | null>;
}

const collection = await collectionService.get(collectionAddress);

Now my collection variable showing in the IDE is Collection type Not Collection | null as I expected.

Not sure if this has something to do with the eslint?

Here is my .eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true
  },
  extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
  overrides: [],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: ['@typescript-eslint'],
  rules: {}
};

CodePudding user response:

You need to set the compilerOptions.strictNullChecks flag in the tsconfig.json to true.

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

The default value of compilerOptions.strictNullChecks is false

  • Related