0

What I'm trying to do is convert a const object into a class so that I can create readonly properties for example. This to ensure that the values never changes.

In my class I'm trying to create a module property which should be defined, but always empty.

I tried a number of different things:

public modules: Readonly<[]> = []; // Error: A tuple type element list cannot be empty.
public readonly modules: IModule[] = []; // Disallows re-assignment, but still allows things such as push, pop etc.

interface EmptyTuple {
  length: 0;
}

// Types of property 'length' are incompatible. Type 'number' is not assignable to type '0'.
public modules: Readonly<EmptyTuple> = [];

The latest one doesn't make any sense since 0 is a number... I really have no idea what I'm doing here really so some guidance would be perfect.

I tried looking at https://github.com/Microsoft/TypeScript/issues/13126 to see if I could find some sort of answer but to no avail.

Does anyone know how to achieve having an empty array that cannot be modified?

5
  • 1
    What problem are you trying to solve that you think this is a solution to? Commented Jun 21, 2018 at 11:53
  • @Ben "What I'm trying to do is convert a const object into a class so that I can create readonly properties for example. This to ensure that the values never changes." Commented Jun 21, 2018 at 12:30
  • 1
    Why? Why do you want to do that? What problem does that solve for you? What is your real problem, that you think this is a solution to? Commented Jun 21, 2018 at 12:49
  • @Ben The project today is very loosely typed, so it's part of multiple steps to make sure that things run as intended since the project is very complicated and getting harder and more time-consuming to debug. Adding this layer of typing will prevent future errors straight away when writing the code, thus increasing the likelihood of doing things the intended way. I guess you could see it as a layer of security as well as act as guidance for other developers in the future. Commented Jun 21, 2018 at 13:12
  • Why not a property get which returns a fresh empty array each time? Commented Jun 21, 2018 at 13:33

3 Answers 3

3

Does anyone know how to achieve having an empty array that cannot be modified?

You can type the field as ReadonlyArray and mark it with readonly keyword:

class Foo {
  readonly modules: ReadonlyArray<{}> = [];
}

const foo = new Foo();

foo.modules = []; //error
foo.modules.push({}) //error
Sign up to request clarification or add additional context in comments.

Comments

2

TypeScript 3.0 now includes better tuple type support:

type EmptyTuple = []

const empty: EmptyTuple = [] // OK

//  Type '[number]' is not assignable to type '[]'.
//    Types of property 'length' are incompatible.
//      Type '1' is not assignable to type '0'.
const err1: EmptyTuple = [1]

Source - TS 3.0 Announcement

Comments

1

You can do things like this to avoid these errors:

    public modules: = any[]; // Error: A tuple type element list cannot be empty.
    public readonly modules: = any[]; // Disallows re-assignment, but still allows things such as push, pop etc.

...

Comments

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.