microsoft / microsoft/TypeScript

Allow functions to have `new symbol` as the return type

Ouverte
#37,469 5 commentaires 59 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Search Terms

  • unique symbol
  • new unique symbol
  • new symbol

Suggestion

I request that unique symbol be allowed as the return type of a function declaration.

Alternatively, it might be better to use new symbol to disambiguate https://github.com/microsoft/TypeScript/issues/40106#issuecomment-677741032.

Use Cases

Currently, it’s impossible to create an alias or a wrapper function for the global Symbol constructor and use that to construct unique symbols:

// ./es-globals/fundamentals.js
export const ESSymbol = Symbol;
export const { for: ESSymbol_for } = ESSymbol;
// ./es-globals/fundamentals.d.ts
export import ESSymbol = globalThis.Symbol;
export declare const ESSymbol_for: typeof ESSymbol.for;
// ./symbols.js
import { ESSymbol, ESSymbol_for } from "./es-globals/fundamentals.js";

// should be `unique symbol`, but is instead `symbol`:
export const customSymbol = ESSymbol("custom");

// should be `unique symbol` or `global symbol "nodejs.util.inspect.custom"`,
// but is instead `symbol`:
export const nodejs_util_inspect_custom = ESSymbol_for("nodejs.util.inspect.custom");

// should be `unique symbol` or `global symbol "nodejs.util.promisify.custom"`,
// but is instead `symbol`:
export const nodejs_util_promisify_custom = ESSymbol_for("nodejs.util.promisify.custom");

Examples

This would allow defining SymbolConstructor as:

declare interface SymbolConstructor {
	/**
	 * Returns a new unique Symbol value.
	 * @param description Description of the new symbol value.
	 */
	(description?: string | number): new symbol;

	/**
	 * Returns a Symbol object from the global symbol registry matching the given key if found.
	 * Otherwise, returns a new symbol with this key.
	 * @param key key to search for.
	 */
	for<T extends string>(key: T): global symbol T;
	// or, until GH-35909 is implemented:
	for(key: string): new symbol;
}

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.

See also

Guide de contribution

Ouvrir le guide de contribution

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.

Piste de recherche

Commencez par examiner les types de retour proposés unique symbol et new symbol, les exemples de SymbolConstructor ainsi que les issues et pull requests associés répertoriés sous « See also ». Déterminez la sémantique de types attendue et le comportement du compilateur concerné ; le travail terminé doit inclure le typage demandé du constructeur et des appels au registre, sans modifier la sortie JavaScript.

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é
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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