microsoft / microsoft/TypeScript
Support custom typeof functions
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les déclarations de type-func.d.ts et la reproduction dans example.ts fournies dans l’issue, puis examinez comment TypeScript gère le narrowing pour les vérifications typeof et les type guards définis par l’utilisateur. C’est terminé lorsque les comparaisons montrées infèrent any[] et number dans leurs branches sans casts, tout en préservant la sortie et le comportement JavaScript existants.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- Active
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100