Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Typescript Class Constraint String

I try to create a class with a string contraint but the it gives an error at the get scale() function.

class Scaling<T extends string> {
  _scale = "";

  constructor(props: T) {
    this._scale = props;
  }

  setScale(scale: T) {
    this._scale = scale;
  }

  get scale(): T {
    return this._scale;
  }

Type 'string' is not assignable to type 'T'. 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'. }

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You should remove explicit T return type from get scale(): T.

Because during initialization, T inferes as literal type of argument. COnsider this example:

class Scaling<T extends string> {
    _scale = "";

    constructor(props: T) {
        this._scale = props;
    }

    setScale(scale: T) {
        this._scale = scale;
    }

    get scale(): T {
        return this._scale;
    }
}

// T infered as literal "hello"
const result = new Scaling('hello')

Hence, when you want to return T it should be "hello".

In your example, it can't be "hello" because default value of _scale is empty string and accordingly it is infered as a string.

let str = ''

// Type 'string' is not assignable to type '"hello"'
const sameCase = (): 'hello' => str 

You can't use T as an explicit type for _scale because _scale is mutable and types are immutable.

This is why it is unsafe to return T type from get scale

Even if i remove the T from the get scale, i still get an error when for const result = new Scaling("hello"); result.setScale("other")

My bad, did not check it.

In order to make this class generic we need to convert infered T from more specific type to more wider.

type Infer<T> = T extends infer R ? R : never

class Scaling<T extends string> {
  _scale = "";

  constructor(props: Infer<T>) {
    this._scale = props;
  }

  setScale(scale: T) {
    this._scale = scale;
  }

  get scale() {
    return this._scale;
  }
}

// T infered as literal "hello"
const result = new Scaling('hello') // ok

result.setScale('123') // ok

Playground

about 4 years ago · Juan Pablo Isaza Denunciar

0

Shouldn't _scale be of type T? So you'd assign with a colon (:)

class Scaling<T extends string> {
  _scale: T;

  constructor(props: T) {
    this._scale = props;
  }

  setScale(scale: T) {
    this._scale = scale;
  }

  get scale(): T {
    return this._scale;
  }
}

The usage would be:

const scaling = new Scaling('Hello World')
console.log(scaling)
about 4 years ago · Juan Pablo Isaza Denunciar

0

The _scale member should really be of type T. For example:

class Scaling<T extends string> {
  _scale = "" as T;

  constructor(props: T) {
    this._scale = props;
  }

  setScale(scale: T) {
    this._scale = scale;
  }

  get scale(): T {
    return this._scale;
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda