Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  23] [ 7]  / answers: 1 / hits: 22117  / 8 Years ago, wed, august 24, 2016, 12:00:00

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?


More From » typescript

 Answers
8

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.


[#60936] Sunday, August 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;