Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 1.61 KB

File metadata and controls

60 lines (46 loc) · 1.61 KB

1015 - Parameter cannot have question mark and initializer.

🔍 Regex Patterns

regexFind: /([a-zA-Z_$][a-zA-Z0-9_$]*)\?\s*:\s*([^=()]+?)\s*=\s*([^,)]+)/
regexReplace: $1: $2= $3

💡 Suggestion

Remove the question mark from optional parameters that have default values, or remove the default value from optional parameters

📝 Examples

Example 1: Optional parameter with initializer

- function greet(name?: string = "Guest") {
+ function greet(name: string = "Guest") {
  console.log(`Hello, ${name}!`)
}

Explanation: Parameters with default values are automatically optional, so the '?' is redundant

Example 2: Arrow function parameter with initializer

- const greetArrow = (name?: string = "Guest") => {
+ const greetArrow = (name: string = "Guest") => {
  console.log(`Hello, ${name}!`)
}

Explanation: Arrow function parameters with default values don't need the '?'

🖼️ Visual Output

Command

npx tsc ./docs/1015/index.ts --noEmit --pretty

Result

docs/1015/index.ts:2:16 - error TS1015: Parameter cannot have question mark and initializer.

2 function greet(name?: string = "Guest") {
                 ~~~~

docs/1015/index.ts:7:21 - error TS1015: Parameter cannot have question mark and initializer.

7 const greetArrow = (name?: string = "Guest") => {
                      ~~~~

OR (without --pretty flag):

docs/1015/index.ts(2,16): error TS1015: Parameter cannot have question mark and initializer.
docs/1015/index.ts(7,21): error TS1015: Parameter cannot have question mark and initializer.