microsoft / microsoft/TypeScript
filename literal string type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Suggestion
🔍 Search Terms
importmetaurl__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)
}
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando il comportamento proposto di FILENAME_TYPE, compresa la sua relazione con --project, __filename e import.meta.url, e confronta gli esempi motivanti del router. Definisci cosa deve fornire un design efficace per percorsi come path/to/$name.tsx senza modificare il JavaScript emesso né il comportamento a runtime.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100