Search code examples
typescripttdd

How to specify length of undefined array in TypeScript?


I am just beginning to learn TypeScript and am working on some TDD stuff. In my model file I have a prop called 'innovatorQuotes' that is an array (made up of strings) that I want to have a specific length of 3. I can't figure out how to achieve this. Here is the current line:

innovatorQuotes: types.optional(types.array(types.string), []),

How can I set the expected length to 3? Thanks in advance!


Solution

  • You could try something like this:

    innovatorQuotes: [string, string, string]
    

    But remember TypeScript doesn't apply to runtime, it can only ensure you don't define an array of different length, but if you do .push etc... it won't be able to recognize it's exceeding the length.