microsoft / vscode-typescript-tslint-plugin

VS Code extension that provides TSLint support using the typescript-tslint-plugin
https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-tslint-plugin
MIT License
188 stars 35 forks source link

TSLint error Object is possibly 'undefined' rule doesn't work on test file using VSCode #123

Closed pebie closed 3 years ago

pebie commented 4 years ago

Hi,

In my app I've split code and tests files in 2 separated folders

- app
     | -- index.ts
- test
     | -- index.test.ts
- tsconfig.json
- tslint.json

tslint.json:

{
    "extends": ["tslint:latest", "tslint-config-prettier"],
    "linterOptions": {
        "exclude": ["**/*.js"]
    },
    "rules": {
        "object-literal-sort-keys": false,
        "ordered-imports": false,
        "member-ordering": false,
        "curly": [true, "ignore-same-line"],
        "interface-over-type-literal": false,
        "no-string-literal": false,
        "no-implicit-dependencies": [true, "dev"],
        "max-classes-per-file": false,
        "one-variable-per-declaration": false,
        "no-duplicate-imports": true,
        "interface-name": false
    }
}

tsconfig.json

{
    "compilerOptions": {
        "incremental": true,
        "outDir": "./build",
        "allowJs": true,
        "target": "es2018",
        "module": "commonjs",
        "lib": ["es2018"],
        "alwaysStrict": true,
        "sourceMap": true,
        "skipLibCheck": true,
        "noUnusedParameters": false,
        "noUnusedLocals": false,
        "strictNullChecks": true,
        "esModuleInterop": true
    },
    "exclude": ["./node_modules", "./app/static"],
    "include": ["./app/**/*"]
}

TSLint version : 5.18 TS version : 3.5.3

app/index.js

type Test = {
    toto?: {
        tata: {
            truc: string | undefined
        }
    }
}
const test: Test = {}
// Got TSLint errors 👍
// Object is possibly 'undefined' (indeed toto may be undefined)
const tata: string = test.toto.tata.truc 
...

tests/index.js

describe('foo', () => {
        it('bar', () => {
            type Test = {
                toto?: {
                    tata: {
                        truc: string | undefined
                    }
                }
            }

            const test: Test = {}
            // No lint error 👎 
            const tata: string = test.toto.tata.truc
            expect(tata)
        })
    })
...

NB:

My expected behavior is that error shows up in the Problems panel and highlights as well in editor.

EDIT: It seems the issue provide from strictNullChecks props

pebie commented 4 years ago

I think the only workarround is to use a different tsconfig file https://github.com/Microsoft/TypeScript-Sublime-Plugin/issues/605

mjbvz commented 4 years ago

Are you sure the object is possibly undefined error comes from TSLint? That seems like a normal TS error

mjbvz commented 3 years ago

Closing as this error sounds like it comes from TypeScript instead of being TSLint specific