microsoft / microsoft/TypeScript

Common property check isn't performed when the target has any index signature

Ouverte
#55,709 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Domain: check: Excess Property Checking Possible Improvement
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔎 Search Terms

common property check weak type index signature

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

https://www.typescriptlang.org/play?noErrorTruncation=true&ts=5.3.0-dev.20230911#code/C4TwDgpgBAIhA2BDEAeAKnJIoQB7AgDsATAZylOACcBLQgcwD4oBeKDBZKAHykIFcAtgCMIVANwAoSXQJUAZogDG0ALLKAFnQgBhAPaF5Neukxc8BEuUq0GzAN6SoUUJFIB+AFxR+hANaEegDuhFLOiPJyXlCOzs4A2gDSUHSwnKgcWIwAutECImJhUAC+UsXSxBBKSFTQ8r5KwDQGLhCUKE7sZtgWRGQU1HT0ADSdaPqGxjj4feTqSlqEugZGJpnIjKOMABRKK8be9i7gbYdQlVik3uvYxSVQAGTsE6sAlN4Abno0xFKSBJRtrFjm5DndEORgc4LsgrlAAEQGaCkKoGYjwngIpFQQR0fgEeFFYqjcKRMSHTrOAD0VKgAAFgKQALR4SCNFlUKh6KiUqB6PRgUgAfWAWmFNGFvgCwUIQphIG8AEYAAzKkklUbFV5-eqERrNQitSgAJnQ00s-SO8Vw3gABj8mQASez5URUYq27LeGxDEo7RDXV4xcoA4DGoGdGn0xks3Bs4Acrk85xURAkPSCbzUfgQTXa6S6-UtUMAZjNvSsMSg1u9IBEengXoGtnofu2AfYQfsIbawBLEeptIZzNZVQTYiTnVT6czLioObzUiAA

💻 Code
type Delay<TDelay extends string> = TDelay | number;

interface MachineConfig<TDelay extends string> {
  types?: unknown;
  after?: {
    [K in Delay<TDelay>]?: number;
  };
}

declare function test<
  TDelay extends string,
  TConfig extends MachineConfig<TDelay>,
>(config: { types: { delays: TDelay } } & TConfig): void;

test({
  types: {} as {
    delays: "one second" | "one minute";
  },
  after: {
    // @ts-expect-error
    oops_this_is_unknown_delay: 100,
  },
});

function test2<T extends { [x: `id-${number}`]: string }>(a: T) {}
test2({
  // @ts-expect-error
  random: true,
});

function test3<T extends { [x: symbol]: string }>(a: T) {}
test3({
  // @ts-expect-error
  random: true,
});
🙁 Actual behavior

No errors are raised here since index signatures turn off the common property check completely.

🙂 Expected behavior

I don't have a good intuition about the common property check so I'm not sure if all of those 3 examples should be treated as bugs/possible improvements. I think though that especially the first one is surprising because I have a list of 2 concrete string properties that are meant to be allowed there and yet the unrelated~ index signature for numbers turns off this check, leading to accidental problems at runtime.

Additional information about the issue

No response

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par le TypeScript Playground associé et comparez les trois exemples dans test, test2 et test3. Suivez le comportement de la vérification des propriétés communes avec des signatures d’index mappées, de template literal et de symbole ; le travail est terminé lorsque les diagnostics attendus et le traitement prévu des trois cas sont établis.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.