0

The type in the constructor has two default values active or finished. But I think there should be a parameter type such as number or string.

Any explanation about this?

class ProjectList{
    templateElement: HTMLTemplateElement;

    constructor(private type: 'active' | 'finished'){
    
    }
}
phentnil
  • 2,195
  • 2
  • 14
  • 22
Oliver
  • 191
  • 1
  • 10
  • 2
    The parameter type *is* `'active' | 'finished'`. It's not a default value - it's that those are the only two possible arguments the function can be provided with. `number` or `string` would both be outside the permitted values. – CertainPerformance Mar 18 '22 at 03:22
  • 1
    See [Literal Types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) and scroll down to _"literal unions"_ – Phil Mar 18 '22 at 03:26

1 Answers1

0

You are declaring that type is a string restricted to only possible 2 values. A default can also be declared, like this:

class ProjectList{
    templateElement: HTMLTemplateElement;

    constructor(private type: 'active' | 'finished' = 'active'){
    
    }
}
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78