2

I am making an Http client. The typescript definition for this would look something like:

declare namespace Http {
   type HttpOptions = ...;
   type HttpPromise<T> = ...

   function get<T>(url: string, options?: HttpOptions): HttpPromise<T>;
   function delete<T>(url: string, options?: HttpOptions): HttpPromise<T>;
}

The delete here now is yelling because this is a reserved word. But I do have a method Http.delete('/foo') on my module.

How do i declare that dependency?

1 Answer 1

5

I'm guessing you don't want to hear "don't do that", but that's probably the best advice. Using reserved words as identifiers isn't guaranteed to fail, but you shouldn't be surprised if some JavaScript environment somewhere balks at it. But never mind, we're going ahead with it.


So this is a bit of a strange one. I'd like to use a quoted string literal the way you can with a method or property name, as mentioned in the TypeScript spec:

String literals may be used to give properties names that are not valid identifiers

But that doesn't work for function names, which must be valid identifiers.

One thing you can apparently do is give the function a valid name and then export an alias to it:

declare namespace Http {
  export type HttpOptions = ...
  export type HttpPromise<T> = ...
  export function get<T>(url: string, options?: HttpOptions): HttpPromise<T>;
  function del<T>(url: string, options?: HttpOptions): HttpPromise<T>;  
  export { del as delete }; // no error
}

I say you can "apparently" do it because I can't find any documentation that indicates one way or another. I would have thought that the reserved word would fail in the as clause (and you can't quote it there either; del as "delete" is an error), but it seems to work:

Http.get('url') // okay
Http.delete('url') // apparently okay but you probably shouldn't
Http['delete']('url') // okay

Does that help?


Another idea is to use declaration merging, but again I'm kind of surprised it works. First declare the namespace with only the type aliases, and then merge in a declared constant of the same name with the properties. It's fragile and weird but it also works for me:

declare namespace Http {
  export type HttpOptions = ...
  export type HttpPromise<T> = ...
}
declare const Http: {
  get<T>(url: string, options?: Http.HttpOptions): Http.HttpPromise<T>;
  "delete"<T>(url: string, options?: Http.HttpOptions): Http.HttpPromise<T>;  
}

Hope one of those works for you; good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try your second approach. I specifically writing a custom wrapper for the github.com/axios/axios library. So the delete function already exists. I can create an alias del or remove or something but rather not.
Exporting aliases is exactly what I was looking for. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.