microsoft / microsoft/TypeScript

Correctly handle generic functions (e.g.: `Object.freeze`) passed as callbacks to generic functions (e.g.: `Array.prototype.map`)

Abierto
#42,862 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Has Repro In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Suggestion

🔍 Search Terms

  • array map generic callback
  • object.freeze array map

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

A generic transforming function (e.g.: Object.freeze) passed as a callback to a generic mapping function (such as Array.prototype.map), should be handled similarly to an arrow function with inferred types.

📃 Motivating Example

From engine262/src/engine.mjs:

// @target: ES2015
// @filename: index.ts
export const FEATURES = Object.freeze([
//                 ^?
	{
		name: 'Top-Level Await',
		flag: 'top-level-await',
		url: 'https://github.com/tc39/proposal-top-level-await',
	},
	{
		name: 'Hashbang Grammar',
		flag: 'hashbang',
		url: 'https://github.com/tc39/proposal-hashbang',
	},
	{
		name: 'RegExp Match Indices',
		flag: 'regexp-match-indices',
		url: 'https://github.com/tc39/proposal-regexp-match-indices',
	},
	{
		name: 'FinalizationRegistry.prototype.cleanupSome',
		flag: 'cleanup-some',
		url: 'https://github.com/tc39/proposal-cleanup-some',
	},
	{
		name: 'Arbitrary Module Namespace Names',
		flag: 'arbitrary-module-namespace-names',
		url: 'https://github.com/tc39/ecma262/pull/2154',
	},
	{
		name: 'At Method',
		flag: 'at-method',
		url: 'https://github.com/tc39/proposal-item-method',
	},
].map(Object.freeze));

Workbench Repro

Expected type:
export const FEATURES: readonly {
	readonly name: string;
	readonly flag: string;
	readonly url: string;
}[];
Like when using an arrow function:
// @target: ES2015
// @filename: index.ts
export const ARROW_FEATURES = Object.freeze([
//                       ^?
	{
		name: 'Top-Level Await',
		flag: 'top-level-await',
		url: 'https://github.com/tc39/proposal-top-level-await',
	},
	{
		name: 'Hashbang Grammar',
		flag: 'hashbang',
		url: 'https://github.com/tc39/proposal-hashbang',
	},
	{
		name: 'RegExp Match Indices',
		flag: 'regexp-match-indices',
		url: 'https://github.com/tc39/proposal-regexp-match-indices',
	},
	{
		name: 'FinalizationRegistry.prototype.cleanupSome',
		flag: 'cleanup-some',
		url: 'https://github.com/tc39/proposal-cleanup-some',
	},
	{
		name: 'Arbitrary Module Namespace Names',
		flag: 'arbitrary-module-namespace-names',
		url: 'https://github.com/tc39/ecma262/pull/2154',
	},
	{
		name: 'At Method',
		flag: 'at-method',
		url: 'https://github.com/tc39/proposal-item-method',
	},
].map((feature) => Object.freeze(feature)));

Workbench Repro

Actual type:
export const FEATURES: readonly Readonly<unknown>[];

💻 Use Cases

Getting correct type inference for JavaScript code on the wild web.

Related issues

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la reproducción de index.ts del issue y compara el resultado de map(Object.freeze) con el ejemplo de arrow function; engine.mjs es el uso que motiva esto. Confirma que la implementación produce el tipo esperado de array de objetos readonly en lugar de readonly Readonly[] para el caso de callback genérico.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
28/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.