I have a struct like so:
struct tTest{
char foo [1+1];
char bar [64];
};
In TypesScript I have
export interface tTest{
foo: string;
bar: string;
}
Is there a way to add [64] and [1+1] to the type?
To get the length of a String in TypeScript, you can use length property of the String object. When you use the length property on the String object, it returns an integer that represents the length of the string.
A TypeScript Interface can include method declarations using arrow functions or normal functions, it can also include properties and return types. The methods can have parameters or remain parameterless.
As the comments say: js/ts don't support the char type and there's no way to declare array/string lengths.
You can enforce that using a setter though:
interface tTest {
foo: string;
}
class tTestImplementation implements tTest {
private _foo: string;
get foo(): string {
return this._foo;
}
set foo(value: string) {
this._foo = value;
while (this._foo.length < 64) {
this._foo += " ";
}
}
}
(code in playground)
You'll need to have an actual class as the interfaces lacks implementation and doesn't survive the compilation process.
I just added spaces to get to the exact length, but you can change that to fit your needs.
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