Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

18047 - '{0}' is possibly 'null'

🔍 Regex Patterns

regexFind: /([a-zA-Z_$][a-zA-Z0-9_$]*)\.(length|toUpperCase|trim|\w+)/
regexReplace: $1?.$2

💡 Suggestion

Add null check or use optional chaining to handle the possibility of null values. Use nullish coalescing operator (??) or optional chaining (?.) for safe property access.

📝 Examples

Example 1: Property access on possibly null value

function processValue(value: string | null) {
+  if (value === null) {
+    return
+  }
  console.log(value.length)
  const upper = value.toUpperCase()
  return value.trim()
}

Explanation: Add null check before accessing properties

Example 2: Using optional chaining

-  const result = data.trim()
+  const result = data?.trim() ?? ''

Explanation: Use optional chaining and nullish coalescing

🖼️ Visual Output

Command

npx tsc ./docs/18047/index.ts --noEmit --pretty --strict

Result

docs/18047/index.ts:2:15 - error TS18047: 'value' is possibly 'null'.

2   console.log(value.length)
                ~~~~~

docs/18047/index.ts:3:17 - error TS18047: 'value' is possibly 'null'.

3   const upper = value.toUpperCase()
                  ~~~~~

docs/18047/index.ts:4:10 - error TS18047: 'value' is possibly 'null'.

4   return value.trim()
         ~~~~~

OR (without --pretty flag):

docs/18047/index.ts(2,15): error TS18047: 'value' is possibly 'null'.
docs/18047/index.ts(3,17): error TS18047: 'value' is possibly 'null'.
docs/18047/index.ts(4,10): error TS18047: 'value' is possibly 'null'.