microsoft / microsoft/TypeScript
Support custom typeof functions
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
- custom typeof
- custom typeof function
- custom type guard
Suggestion
When using a custom typeof‑like function, I’d like TypeScript compiler to be able to infer the correct type in the scope guarded by the custom typeof‑like function.
Use Cases
I need this to provide proper type information for the type function from Blissfuljs and similar projects.
The current approach requires defining the type(…) function as type(obj: any): string and then doing a type cast every time something is accessed within the if block:
/// <reference types="blissfuljs"/>
declare var something: any;
if ($.type(something) === "array") {
(something as any[]).forEach(v => {/* stuff */})
}
Examples
type-func.d.ts
/**
* @param obj The variable to check the type of.
* @return The result of `typeof obj` or the class name in lowercase for objects.
* In the case of numbers, if the value is `NaN`, then the result is `nan`.
*/
declare function type(obj: null): "null";
declare function type(obj: undefined): "undefined";
// This is to ensure that the type system short-circuits when it encounters primitive types.
declare function type(obj: number): "number" | "nan";
declare function type(obj: string): "string";
declare function type(obj: symbol): "symbol";
declare function type(obj: boolean): "boolean";
// Needed to ensure proper return values when wrapper objects are used.
/* tslint:disable:ban-types */
declare function type(obj: number | Number): "number" | "nan";
declare function type(obj: string | String): "string";
declare function type(obj: symbol | Symbol): "symbol";
declare function type(obj: boolean | Boolean): "boolean";
declare function type(obj: Function): "function";
/* tslint:enable:ban-types */
declare function type(obj: any[]): "array";
declare function type(obj: RegExp): "regexp";
declare function type(obj: any): string;
export = type;
example.ts
import type = require("./type-func");
declare var something: any;
if (type(something) === "array") {
// $ExpectType any[]
something;
} else if (type(somthing) === "number") {
// $ExpectType number
something;
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 dalle dichiarazioni di type-func.d.ts e dalla riproduzione in example.ts presenti nell’issue, quindi esamina come TypeScript gestisce il narrowing per i controlli typeof e i type guard definiti dall’utente. Il lavoro è completato quando i confronti mostrati inferiscono any[] e number all’interno dei rispettivi rami senza cast, preservando al contempo l’output e il comportamento JavaScript esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100