13

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?

5
  • No, there's nothing for that in js/ts. But what are you trying to do? To limit the length of the strings or do they should have a fixed size? Commented Aug 24, 2016 at 7:40
  • I'm not entirely familiar with what [1+1] would actually do, but it sounds to me like the right way to do that in Typescript would be to define a class tTest with a get foo(): string method which returns your padded/truncated value. Commented Aug 24, 2016 at 7:40
  • 'char' isn't a type in JS nor TS, only string exists. What you can do is either limit the length of strings, or use an array of integers where each entry represents a character. Commented Aug 24, 2016 at 7:41
  • @NitzanTomer They need to have fixed length. Let's say foo should have length 64. If the string I pass to foo only has a length of 43, I need this to be adjusted by adding stuff (ASCIIZ e.g.). So that every foo, no matter what I pass into my interface, is of length 64. Is this clear? I'm not a native speaker. Commented Aug 24, 2016 at 7:50
  • Wrt. struct support in TypeScript: See github.com/Microsoft/TypeScript/issues/24007 Commented May 18, 2018 at 21:00

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was afraid I'd need to do this.. But thanks to you.
2

You can't force the length of an array in Typescript, as you can't in javascript.
Let's say we have a class tTest as following:

class tTest{
       foo = new Array<string>(2);
};

As you can see, we have defined an array of string with length 2, with this syntax we can restrict the type of values we can put inside our array:

let t = new tTest();
console.log('lenght before initialization' +  t.foo.length);

for(var i = 0; i < t.foo.length; i++){
    console.log(t.foo[i]); 
}

t.foo[0] = 'p';
t.foo[1] = 'q';
//t.foo[2] = 3; // you can't do this
t.foo[2] = '3'; // but you can do this

console.log('length after initialization' +  t.foo.length);

for(var i = 0; i < t.foo.length; i++){
    console.log(t.foo[i]); 
}

In this manner we can't put a number value inside your array, but we can't limit the number of values you can put inside.

Playground

1 Comment

Limiting the number of values doesn't fit my needs.. Looks like I have to do what I don't want to. But thanks to you aswell!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.