microsoft / microsoft/TypeScript

Methods in simple mixin result as type any.

Abierto
#39,943 3 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

TypeScript Version: 3.9.2

Search Terms:

typescript mixin method is any

Code

type AnyCtor = new (...a: any[]) => any

function Foo<T extends AnyCtor>(Base: T) {
	return class Foo extends Base {
		foo() {}
	}
}
function Bar<T extends AnyCtor>(Base: T) {
	return class Bar extends Base {
		bar() {}
	}
}
function One<T extends AnyCtor>(Base: T) {
	return class One extends Base {
		one() {}
	}
}
function Two<T extends AnyCtor>(Base: T) {
	return class Two extends Base {
		two() {}
	}
}
function Three<T extends AnyCtor>(Base: T) {
	return class Three extends One(Two(Base)) {
		three() {}
	}
}

class MyClass extends Three(Foo(Bar(Object))) {
	test() {
		// @ts-expect-error
		this.foo(123)
		// @ts-expect-error
		this.bar(123)
		// @ts-expect-error // ERROR
		this.one(123) // this.one is type `any`!
		// @ts-expect-error // ERROR
		this.two(123) // this.two is type `any`!
		// @ts-expect-error
		this.three(123)
		console.log('no runtime errors')
	}
}

const m = new MyClass()
m.test()

Expected behavior:

There should be an error on all the lines marked with // @ts-expect-error

Actual behavior:

There is no type error on the lines with this.one and this.two because this.one and this.two are seen as type any.

The expectation is that the one and two methods have the proper type (with zero parameters) which would therefore cause a type error from passing in arguments.

Playground Link

Related Issues:

Someone from the Discord chat thought perhaps https://github.com/microsoft/TypeScript/pull/29571 might be related, but not with 100% certainty. They do however think this is a bug.

Ultimately, making mixins in TypeScript is too hard. It's too easy to get them wrong and they become a very inconvenient in TypeScript (whereas they are very convenient in plain JavaScript).

Also related: https://github.com/microsoft/TypeScript/issues/32080

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la reproducción vinculada en Playground y compara el comportamiento tratado en los issues 32080 y pull request 29571. Rastrea cómo las llamadas anidadas a mixins infieren los tipos de one y two; se considera terminado cuando las seis líneas @ts-expect-error producen errores de tipo, incluidas las llamadas con argumentos adicionales.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.