1
  • I can assign a unique index to a python class data member like here.
  • Is there an equivalent in typescript?
class Person
{
    index: number
    age: number
    name: string

    constructor(age: number, name: string)
    {
        // this.index = fresh index every time 
        this.age = age;
        this.name = name;
    }
}
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • Have you tried using a static property or using a randomly generated unique index? If you think that it might help, I write an answer. – mahooresorkh Oct 30 '22 at 07:16

3 Answers3

1

As you figured out, there is no built-in mechanism for auto-incrementing class instance index feature.

As the previous answers and comment suggest, a possible solution, common in OOP, is to use a static counter, and incrementing it in the class constructor.

JavaScript/TypeScript being not necessarily OOP, like Python, another typical solution is also to use a closure (instead of the static counter):

let counter = 0; // In closure of Person class

class Person {
    index: number
    age: number
    name: string

    constructor(age: number, name: string) {
        this.index = counter++;
        this.age = age;
        this.name = name;
    }
}

var p1 = new Person(55, "moish");
var p2 = new Person(74, "uzi");
console.log(p1); // index 0
console.log(p2); // index 1

Playground Link


A common factorization is to separate this auto-incrementing index feature into a base class:

let counter = 0; // In closure of WithAutoIncrementedIndex class

class WithAutoIncrementedIndex {
    index: number;

    constructor() {
        this.index = counter++;
    }
}

// All classes that extend WithAutoIncrementedIndex will have
// the auto-incrementing index feature,
// but sharing the same counter
class Person extends WithAutoIncrementedIndex {
    age: number
    name: string

    constructor(age: number, name: string) {
        super(); // Call the parent constructor
        this.age = age;
        this.name = name;
    }
}

var p1 = new Person(55, "moish");
var p2 = new Person(74, "uzi");
console.log(p1); // index 0
console.log(p2); // index 1

Playground Link


BTW, in your constructor, you do not need to explicitly assign your class members if they are already used as constructor parameters with the same name and a visibility modifier, see Class Parameter Properties:

TypeScript offers special syntax for turning a constructor parameter into a class property with the same name and value. These are called parameter properties and are created by prefixing a constructor argument with one of the visibility modifiers public, private, protected, or readonly.

So you can do directly:

class Person extends WithAutoIncrementedIndex {
    constructor(public age: number, public name: string) {
        super(); // Call the parent constructor
    }
}

Playground Link

ghybs
  • 47,565
  • 6
  • 74
  • 99
0

Following the suggestion of @mahooresorkh, using static solves it:

class Person
{
    index: number
    age: number
    name: string

    // arrggghh this is ugly 
    static gIndex: number = 0;
    static getFreshIndex(): number
    {
        Person.gIndex += 1;
        return Person.gIndex; 
    }

    constructor(age: number, name: string)
    {
        this.index = Person.getFreshIndex();
        this.age = age;
        this.name = name;
    }
}

var p1 = new Person(55, "moish");
var p2 = new Person(74, "uzi");
console.log(p1);
console.log(p2);

I wonder if there's a simpler way (?)

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
0

Well, no, there's no simplier way that using a static member

class Person {
  // 0~1 number
  numberRandomUid = Math.random()
  // 6-char [0-9a-z] string
  stringRandomUid = Math.random().toString(36).slice(2, 8)
  // non-serializable
  symbolRandomUid = Symbol()

  // ordered int, dunno how will work with inheritance
  static gIndex = 0;
  numberOrdered = Person.gIndex ++;
  // or numberOrdered = ++ Person.gIndex;
  // or numberOrdered = Person.gIndex += 1;


  constructor(
    public age: number, 
    public name: string) { }
}
Dimava
  • 7,654
  • 1
  • 9
  • 24