microsoft / microsoft/TypeScript
Statically match namespace names using `new Function().name`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
I have this bit of code:
import * as express from 'express';
import {RequestHandler} from 'express';
const router = express.Router();
export const register = (v: any) => {
router.get('/', makeGetFoo(v));
router.put('/', makePutFoo(v));
};
namespace ApiDoc {
export interface makeGetFoo { // <--- i want to share this interface with front-end codebase
success: boolean
}
export interface makePutFoo { // <--- i want to share this interface with front-end codebase
success: boolean
}
}
const makeGetFoo = (v: any): RequestHandler => {
return (req, res, next) => {
res.json(<ApiDoc[makeGetFoo.name]>{success: true});
};
};
const makePutFoo = (v: any): RequestHandler => {
return (req, res, next) => {
res.json(<ApiDoc[makePutFoo.name]>{success: true});
};
};
my goal is to put the typings in a intermediary file, so that the types can be sourced by the front-end code. Hopefully you understand what I am trying to do.
The problem is I get this:

Cannot use namespace ApiDoc as a type
What I want to do is create a system where the interface name matches the function name so that I don't have to guess, etc.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TypeScript example and the reported “Cannot use namespace ApiDoc as a type” error. Determine whether the requested function-name-based interface lookup is a compiler feature and define the expected type-checking behavior; done should allow the intended shared typings without that error, with tests covering the demonstrated pattern.
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
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100