microsoft / microsoft/TypeScript

filename literal string type

Offen
#43,025 3 Kommentare 15 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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)
}

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Überprüfung des vorgeschlagenen Verhaltens von FILENAME_TYPE, einschließlich seiner Beziehung zu --project, __filename und import.meta.url, und vergleichen Sie die zugrunde liegenden Router-Beispiele. Definieren Sie, was ein erfolgreiches Design für Pfade wie path/to/$name.tsx bereitstellen muss, ohne das erzeugte JavaScript oder das Laufzeitverhalten zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.