f.e., I have
declare class Foo extends Bar {
foo: number
}
How do I declare that foo
has a default value (or initial value) of, say, 60.
I tried
declare class Foo extends Bar {
foo: number = 60
}
but I get an error like
4 foo: number = 60
~~
path/to/something.js/Foo.d.ts/(4,28): error TS1039: Initializers are not allowed in ambient contexts.
To set default value for an object parameter:Type the object as having one or more optional properties. Set default value for each of the optional properties. Alternatively, set the entire object as optional, by setting all its properties to optional.
Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.
The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.
In TypeScript, interfaces represent the shape of an object. They support many different features like optional parameters but unfortunately do not support setting up default values. However, you can still set up a TypeScript interface default value by using a workaround.
Try removing declare from your class definition. By using declare it will define a class type. The type is only defined, and shouldn't have an implementation.
class Foo extends Bar {
foo: number = 60
}
Your program attempts to perform two mutually contradictory tasks.
You need to determine which of these tasks you wish to perform and adjust your program accordingly by removing either the initializer or the declare
modifier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With