microsoft / microsoft/TypeScript
Support custom typeof functions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the type-func.d.ts declarations and example.ts reproduction in the issue, then inspect how TypeScript handles narrowing for typeof checks and user-defined type guards. Done means the shown comparisons infer any[] and number within their branches without casts, while preserving existing JavaScript output and behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100