microsoft / microsoft/TypeScript

filename literal string type

Open
#43,025 3 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed FILENAME_TYPE behavior, including its relationship to --project, __filename, and import.meta.url, and compare the motivating router examples. Define what a successful design must provide for paths such as path/to/$name.tsx without changing emitted JavaScript or runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.