microsoft / microsoft/TypeScript
TypeScript unable to infer types properly between interfaces and overloaded methods.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Bug Report
In all versions of TS I can find, there's no way to mark certain call patterns of a method as deprecated while maintaining inference with certain interfaces. If an interface is split out into different overloads so certain overloads can be deprecated, then it breaks inference in some cases.
🔎 Search Terms
I'm not sure what to search for here. Interfaces?
🕗 Version & Regression Information
- All versions
⏯ Playground Link
Playground link with relevant code
💻 Code
interface Observer<T> {
next(value: T): void;
error(error: any): void;
complete(): void;
}
interface Unsubscribable {
unsubscribe(): void;
}
interface Subscribable<T> {
subscribe(observer?: Partial<Observer<T>>): Unsubscribable;
}
class Observable<T> implements Subscribable<T> {
/////////////////////////////////////////
// Valid call pattern
/////////////////////////////////////////
subscribe(observerOrNext?: Partial<Observer<T>> | ((value: T) => void)): Unsubscribable;
//////////////////////////////////////////
// Deprecated call patterns
//////////////////////////////////////////
/**
* @deprecated This call pattern is going away
*/
subscribe(next: null | undefined, error: null | undefined, complete: () => void): Unsubscribable;
/**
* @deprecated This call pattern is going away
*/
subscribe(next: null | undefined, error: (error: any) => void, complete?: () => void): Unsubscribable;
/**
* @deprecated This call pattern is going away
*/
subscribe(next: (value: T) => void, error: null | undefined, complete: () => void): Unsubscribable;
/**
* @deprecated This call pattern is going away
*/
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Unsubscribable;
////////////////////////////////////////////
// Impl
////////////////////////////////////////////
subscribe(...args: any[]): Unsubscribable {
return {
unsubscribe() {}
};
}
}
function somethingThatTakesSubscribable<T>(arg: Subscribable<T>): T {
return null!;
}
/////////////////////////////////////
// Our failed expectation here
// (it's unknown)
/////////////////////////////////////
const result = somethingThatTakesSubscribable(new Observable<number>()); // $ExpectType number
🙁 Actual behavior
result is unknown above.
🙂 Expected behavior
result would be number.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el TypeScript Playground enlazado y reproduce el resultado de inferencia para Observable pasado a somethingThatTakesSubscribable. Investiga cómo se comparan los overloads divididos, incluidas las firmas obsoletas, con Subscribable. La tarea estará completada cuando el mismo ejemplo infiera result como number en lugar de unknown, con cobertura para el patrón de overloads reportado.
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
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100