question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Statically match namespace names using `new Function().name`

See original GitHub issue

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:

screenshot from 2018-11-14 17-08-22

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
svieiracommented, Nov 15, 2018

@weswigham - Point 1 actually was specified in the 2015 release (unfortunately, you have to search for SetFunctionName to find all the places, though kangax’s ES6 support table has a decent, non-authoritative list of them).

1reaction
weswighamcommented, Nov 15, 2018

I was just briefly confused by trying to puzzle out what you’re asking for. You want two things:

  1. A strong, literally typed Function.name member. (This is actually really hard for things that aren’t just function declarations - that const x = () => void 0 yields a function with name "x" is by runtime convention, not spec, if I remember correctly, and that declaration/function association is heuristic. Still interesting, though. We may have another issue for this.)

  2. The ability to dynamically look up types in a namespace. We can’t use square brackets for that, because we explicitly already reserved it to decompose types (ie, lookup type members), rather than lookup namespace members (and we consider those separate scopes).

Neither is particularly easy on their own (and the second would need a proposal of some kind - and a way to reason about it in higher order (ie, with generics)). You can at least avoid the second issue by writing a single interface:

interface ApiDoc {
  makeGetFoo:  {
    success: boolean
  };
  makePutFoo:  {
    success: boolean
  };
}

instead of the namespace, and then ApiDoc["makeGetFoo"] like in your followup.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Statically match namespace names using `new Function().name ...
I have this bit of code: import * as express from 'express'; import {RequestHandler} from 'express'; const router = express.Router(); export const register ......
Read more >
Regex for matching content (function/namespace) between ...
I need to match several things in code files (I have a lot of them, and all of them has similar structure, but...
Read more >
using directive - C# Reference - Microsoft Learn
The using static directive names a type whose static members and nested types you can access without specifying a type name. Its syntax...
Read more >
Google C++ Style Guide
With few exceptions, place code in a namespace. Namespaces should have unique names based on the project name, and possibly its path.
Read more >
FAQ: things you need to know about namespaces - PHP
Example #5 Fully Qualified names. <?php namespace foo; $a = new \my\name(); // instantiates "my\name" class echo \strlen('hi'); // calls function "strlen"
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found