I need to add a member to an HTMLElement
, in other words, I need to store data into an element. This is what I would like to achieve as if I am coding in ScriptSharp.
/** My method */
public DoIt(element: Element, obj: Object) {
Literal("{0}.extended = {1}", element, obj); // This is not standard Typescript!
}
In my example ScriptSharp (a project to convert C# code into Javascript) provides a Script.Literal
object that allows developers to write plain Javascript when a C# abstraction is not possible.
So that the Javascript output is:
// Probably Typescript will render it a bit differently, but basically
// this is what we get in the end...
var _doit = function(element, obj) {
element.extended = obj;
};
How can I achieve this in Typescript? Or maybe I should handle this problem in a different way?
The string literal type allows you to specify a set of possible string values for a variable, only those string values can be assigned to a variable. TypeScript throws a compile-time error if one tries to assign a value to the variable that isn't defined by the string literal type.
Although single quotes and double quotes are the most popular, we have a 3rd option called Backticks ( `` ). Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes.
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.
Any valid JavaScript is also valid TypeScript. This means that you can write literal JS in any place in your code.
var _doit = function(element, obj) {
element.extended = obj;
};
This is valid JS and TS.
However, since you use TypeScript, you may also want to use static typing with your code. If you just add types to your code, it will compile correctly, but you'll get a semantic error:
var _doit = function(element:HTMLElement, obj) {
element.extended = obj; // error: HTMLElement doesn't have property 'extended'
};
To prevent this error, you can notify the compiler that you intend to create a new property on HTMLElement
:
interface HTMLElement {
extended?: any;
}
Now the compiler knows that you have an (optional) property extended
on HTMLElement
and will compile without errors. You will also get code autocompletion on this property (and JSDoc if provided).
This worked for me:
class Foo {
public DoIt(element: Element, obj: Object) {
var anyElement: any = element;
anyElement.extended = obj;
}
}
The problem (as you probably noticed) is that Element does not declare a property with the name extended, so TypeScript does its job and enforces the Element type. If you want to work around this, you can use the any
type to do this.
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