Narrowing of generic this is inconsistent with variable narrowing

Abierto
#64,186 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
typescript
Área
compilers

Línea de trabajo

Comience con el TypeScript Playground enlazado y reduzca los ejemplos de generic-union proporcionados para direct this, aliased this y parameter narrowing. Como en el issue no se identifica ningún archivo del repositorio ni ningún test, localice el punto de entrada del narrowing del compilador y añada un regression test que cubra los casos mostrados. Se considerará terminado cuando el narrowing de direct this y aliased this produzca diagnósticos coherentes.

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

Descripción

Possible Improvement
🔎 Search Terms

generic union constraint narrowing this

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play/?noImplicitReturns=false#code/C4TwDgpgBAKu0F4oEkDMAmKAfKBlYATgNwBQAlgHbAQEBmAhgMbT4FQDeJUUA5gQPYBXMAC4oAIgr8KEcaW4BrSgBMx4gM6E5JAL7kqNBsxQYOXXgOFr1jegBt6BbYpVqyGbXpLKIjBwWhaQQpGYDJpKGB6BQg0dAAKADd7QQgxOIBKMUT+MmVSHz9HQODQ8IooSloaAGkVAB4YAD4klLSOKCUKVVgoHSzYUhIgkLCI6k0YAAsydUaoCAAPam71WHgW4Bn1MRgMs251AHcyYEYpqHit2YA6PiEwfc5ublt1aElpWRFzF6gA4CCAgUeQvN4fGz2RziH5-Q4nM4XK7bG5dZRPX5w8ESTROMQAenxUCkCwIAgIAH5MX9seJ3OgYdS+uY9F4RmVxhBNAAFRz0AC2EGoBHmSxWyjWcEgLX4uwx8NO50u-DulkeBzB9HeEikMkZcP+QqBIMxtMh-n1cOOiqRKrR8oNUDNWgJRJo5IAhEzXlqPvTLX89NxWSRhqUxhUJsAAIJ2Mi+5TTWai5YQVbraXI2ZyjWMaSaKD8KBIa7qUHWxHK1UPB0+7WfPWwuEA42gusQ2wWpt-CtK+J2lS1rG+nEuqCE0me71Okd0jzdl5B5ls8PlSJc4BJ9ToFPiyUbLM7WADOLYKDBHy0SgQZQasi0S6l6vCYsIJAaTvQod5igF5J2VJ0lMEttiIcc3TJfgCExKIYjiQ8MjAid3SgmDtigdR6DCdQry5Ex0CQiDyVNfNgCgRwBCOYtLhPYCmkiUDwMnVC-hbYEGNmQjmOg4NdFDdkI3XHk+UFYUdxgBZU3TKUIBlOUgMwHALwgK8ZFvZ5KgfftnzAV933NL8NVeUioH-QD8Oo-guP4BQYOiWIMH7RCmJszEi0w7DcLWOJrNsmkTIo-gqKQeJaMwBB6Ksly-JeNiKiiidXN4ldRjXKNY3jd5E22cTJL3DNZMPeSLKU7oVOvdTzB-AsixAzjzHvKt7hfN99M-JxvxMsz2lPJAEqJJKXlghyEn4ZzEpi7h3Kw2YvPw3ySN-MjAuCmiFOLSKFtYo12P6wsYpDIA

💻 Code
type Type = I32 | Str;
interface Str {
  group: "none";
  kind: "str";
}
interface I32 {
  group: "scalar";
  kind: "i32";
}

declare function takeI32(value: I32): void;
declare function inferKind<T>(value: { kind: T }): T;

function testThis<T extends Type>(this: T) {
  switch (this.group) {
    case "none":
      return;
    case "scalar":
      switch (this.kind) {
        case "str": // no error?
        case "i32":
      }
  }
}

function testParameter<T extends Type>(o: T) {
  switch (o.group) {
    case "none":
      return;
    case "scalar":
      switch (o.kind) {
        case "str": // error!
        case "i32":
      }
  }
}

function testAliasedThis<T extends Type>(this: T) {
  const o = this;
  switch (o.group) {
    case "none":
      return;
    case "scalar":
      switch (o.kind) {
        case "str": // error!
        case "i32":
      }
  }
}

function testThis2<T extends Type>(this: T): I32 | undefined {
  if (this.group === "scalar") {
    const value: I32 = this; // error
    takeI32(this); // error
    this satisfies I32; // error
    const arrow = (): I32 => this; // error
    return this; // error
  }
}

function testParameter2<T extends Type>(o: T): I32 | undefined {
  if (o.group === "scalar") {
    const value: I32 = o; // ok
    takeI32(o); // ok
    o satisfies I32; // ok
    const arrow = (): I32 => o; // ok
    return o; // ok
  }
}

function testAliasedThis2<T extends Type>(this: T): I32 | undefined {
  const o = this;
  if (o.group === "scalar") {
    const value: I32 = o; // ok
    takeI32(o); // ok
    o satisfies I32; // ok
    const arrow = (): I32 => o; // ok
    return o; // ok
  }
}

🙁 Actual behavior

The errors (or lack of them) are inconsistent between narrowing this directly vs narrowing an "aliased" this (const alias = this;)

🙂 Expected behavior

They should be consistent

Additional information about the issue

No response

Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 15 h
PR fusionados (30 d)
106

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.

Más de microsoft/TypeScript

Todos los issues de microsoft/TypeScript

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.