microsoft / microsoft/TypeScript
'this' implicitly has type 'any' in a function used within a conditional expression
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
this any implicit 2683 widen
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
// @strict: true
interface State {
value: string;
matches(value: string): boolean;
}
declare function macthesState(state: { value: string }, value: string): boolean;
declare function isState(state: unknown): state is State;
// this doesn't work
function test(config: unknown, prevConfig: unknown) {
if (isState(config)) {
return {
...config,
matches: isState(prevConfig)
? prevConfig.matches
: function (value: string) {
return macthesState(this, value);
},
};
}
return config;
}
// this works
function test2(config: State) {
return {
...config,
matches: function (value: string) {
return macthesState(this, value);
},
};
}
🙁 Actual behavior
'this' implicitly has type 'any' because it does not have a type annotation.(2683)
🙂 Expected behavior
inferred this type
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il TypeScript Playground collegato e confronta l'esempio dell'espressione condizionale con la funzione funzionante. Traccia il comportamento del compilatore responsabile del contextual typing della funzione annidata e verifica il risultato in strict mode; il lavoro è completo quando il tipo this inferito corrisponde alla forma State prevista senza un'annotazione esplicita.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 40/100