I have a piece of TypeScript code that I'm having trouble to understand. I am fairly new to TypeScript.
export const TerminalWidgetOptions = Symbol("TerminalWidgetOptions");
export interface TerminalWidgetOptions {
endpoint: Endpoint.Options,
id: string,
caption: string,
label: string
destroyTermOnClose: boolean
}
Could someone tell me what exactly happens in the above code? What I do understand is that an interface of the name TerminalWidgetOptions
is created and it forces the parameters endpoint
, id
, caption
, label
and destroyTermOnClose
upon implementation into a class. I though don't quite understand the above line. So, apparently a constant is created, that can only be set once and then stays that way, right? But how can this constant have the same name as the interface type? The assignment of Symbol("TerminalWidgetOptions");
is clear. What comes from the Symbol function is put into the constant.
Is that more or less correct?
Interface types act like class types. You can declare variables to be of an interface type, you can declare arguments of methods to accept interface types, and you can even specify that the return type of a method is an interface type.
TypeScript Interface TypeTypeScript allows you to specifically type an object using an interface that can be reused by multiple objects. To create an interface, use the interface keyword followed by the interface name and the typed object.
Interfaces in TypeScript can be used similar to any other type annotation. To avoid name conflicts, the community is used to add a prefix to all the interfaces. There is no standard for the naming conventions by the language itself. So, TypeScript will not limit you from choosing any name for your interfaces.
// That said, we recommend you use interfaces over type aliases. Specifically, because you will get better error messages. If you hover over the following errors, you can see how TypeScript can provide terser and more focused messages when working with interfaces like Chicken.
You can have a type and a variable with the same name. Types are erased at compile time, while variables remain. Since types and variables can be used in different contexts there is no name conflict between the two. The only exception is classes, for which the class name represents both the type and the constructor function, so you can't redeclare a variable with the same name as that would conflict with the constructor function at run-time.
In TypeScript you can have the same name for a variable/constant, an interface and even a namespace.
TypeScript understands what you're referring to based on context. Remember that interfaces are just type hints to the compiler and the programmer, they disappear completely when the code is compiled, so there are no conflicts in the resulting JavaScript code.
There is not a use case when what you're referring to with TerminalWidgetOptions
is ambiguous:
class Klass implements TerminalWidgetOptions { // interface
someFunction(TerminalWidgetOptions); // constant
let t = TerminalWidgetOptions; // constant
In fact, when you define a class, you're doing something similar. By defining a class you are both declaring a type and defining a value
const d = Klass; // d now is like the constructor of Klass (a value, something that exists)
doSomething<Klass>(); // Here, Klass is a pure type (an abstraction)
The difference?
So, an interface is just a type. A variable is just a value. A class is both a type (defines the interface of instances of that class) and value (is a function that constructs instances of the type)
Hope this makes it a bit clearer to you
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