microsoft / microsoft/TypeScript

filename literal string type

Abierto
#43,025 3 comentarios 15 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

  • import
  • meta
  • url
  • __filename
  • __dirname

✅ 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

Provide current filename (relative to current --project) as a type

tsconfig.json
path/to/file.ts
// path/to/file.ts
function assert(value: "path/to/file.ts") {}
declare var filename: __FILENAME_TYPE__
assert(filename)

Note: __FILENAME_TYPE__ is a placeholder for whatever makes most sense.

This should be separate from __filename (which is an absolute path that can change depending on the location of a typescript project) and import.meta.url which is a fully qualified URL and may also change at runtime.

📃 Motivating Example

An increasing number of web frameworks are using file system paths as a router with $parameters.tsx and such: Next, Gatsby, Remix, Nuxt, etc

/pages/
  index.ts     -> /
  /nested/
    index.ts   -> /nested
    $param.ts  -> /nested/:param

With the addition of `template literal ${types}` people are now parsing out parameters from runtime routers.

router.get("/path/to/:name", (req) => {
  let name: string = req.params.name // (no any)
})

If the current filename was accessible in typescript, you could strictly type path segments without repeating the path name

// path/to/$name.tsx
import type { Params } from "framework"

export default function renderPage(params: Params<__FILENAME_TYPE__>) {
  let name: string = req.params.name // (no any)
}

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 revisando el comportamiento propuesto de FILENAME_TYPE, incluida su relación con --project, __filename e import.meta.url, y compara los ejemplos motivadores del router. Define qué debe proporcionar un diseño satisfactorio para rutas como path/to/$name.tsx sin cambiar el JavaScript emitido ni el comportamiento en tiempo de ejecución.

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
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.