7.1: destructured parameter with a `= {}` default loses its contextual type (TS7031)

Ouverte
#64,974 1 commentaire 3 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@a-tarasyuk y travaille déjà.

Depuis le 27/10/2026.

  • #64978 par @a-tarasyuk — ouverte

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
53/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
typescript
Domaine
compilers

Piste de recherche

Start by reading the compiler’s contextual parameter typing and destructuring code, then use the issue’s minimal reproduction to compare behavior across the affected 7.1 and working 7.0.2 versions. Add a regression test covering an optional contextually typed parameter with a destructuring pattern and an = {} default; done means both binding elements retain their contextual types without TS7031, while the two control cases still pass.

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

Description

Bug
🔎 Search Terms

TS7031, binding element implicitly has an any type, destructuring parameter default, contextual type, 7.1 regression

🕗 Version & Regression Information
  • This changed between versions 7.0.2 and 7.1.0-beta
  • Still present in 7.1.0-dev.20261026.1
  • 6.0.3 and 7.0.2 report no errors
⏯ Playground Link

No response

💻 Code
// tsconfig: { "compilerOptions": { "strict": true, "noEmit": true, "skipLibCheck": true, "types": [] } }
type Fn = (input?: { channel?: string; minutes?: number }) => number;
export const f: Fn = ({ channel, minutes } = {}) => (channel ? 1 : 0) + (minutes ?? 0);

The error needs all three of: a destructuring pattern, an = {} initializer, and a contextually typed optional parameter. Each of these alone still checks cleanly on 7.1:

type F2 = (input: { channel?: string; minutes?: number }) => number;
export const f2: F2 = ({ channel, minutes }) => (channel ? 1 : 0) + (minutes ?? 0); // no initializer: OK

type G = (input?: { channel?: string }) => number;
export const g: G = (input = {}) => (input.channel ? 1 : 0); // identifier, not a pattern: OK
🙁 Actual behavior
a.ts(2,25): error TS7031: Binding element 'channel' implicitly has an 'any' type.
a.ts(2,34): error TS7031: Binding element 'minutes' implicitly has an 'any' type.
🙂 Expected behavior

No errors: channel and minutes take their types (string | undefined, number | undefined) from the contextual parameter type, as on 6.0.3 and 7.0.2.

Additional information about the issue

Found while trialling 7.1.0-beta on a large monorepo; it fires in test helpers and callbacks typed through an alias (({ a, b } = {}) => … assigned to a function type with an optional object parameter).

Langage dominant
Go
Étoiles
112k
Forks
15.4k
Merge moyen
2 j 21 h
PR mergées (30 j)
126

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

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.

Autres issues de microsoft/TypeScript

Toutes les issues de microsoft/TypeScript

Issues similaires

Plus d'issues Go

Recevez les nouvelles issues par e-mail

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