const testResult = config.layers?.find((layer) => layer.id === 'blah')?.dataSource!;
console.log('testResult: ', testResult);
I have an object named Configuration (referred to as "config"), which contains an array of Layers.
Each layer is assigned an "id".
Despite not having an id labeled "blah", why does the output show testResult: undefined?
I would have anticipated a runtime error instead.
As far as I know, the "!" symbol indicates that the variable cannot be undefined. However, the .find() method returned an undefined object, making ".dataSource" inaccessible.
This situation has left me baffled!
Could there be a configuration setting causing this unexpected behavior?
Below is my tsconfig.json file:
{
"extends": [
"eslint:recommended",
"google",
"react-app",
"plugin:jsx-a11y/recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:prettier/recommended",
"plugin:jest/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "2018",
"sourceType": "module",
"project": ["tsconfig.test.json"]
},
"plugins": ["react", "jsx-a11y", "@typescript-eslint", "prettier", "jest"],
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-empty-interface": "off",
"require-jsdoc": [
"error",
{
"require": {
"FunctionDeclaration": false,
"MethodDefinition": false,
"ClassDeclaration": false,
"ArrowFunctionExpression": false,
"FunctionExpression": false
}
}
],
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error"
}
}