Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Cannot set namespace in TypeScript subclass #393

Open
Bouke opened this issue Oct 10, 2018 · 6 comments
Open

Cannot set namespace in TypeScript subclass #393

Bouke opened this issue Oct 10, 2018 · 6 comments
Labels
Bug Issue relates to a problem in the `ember-ajax` codebase

Comments

@Bouke
Copy link

Bouke commented Oct 10, 2018

I'm extending the AjaxService in TypeScript like this:

# services/ajax.ts
import AjaxService from 'ember-ajax/services/ajax';

export default class Ajax extends AjaxService {
    namespace = '/api';
}

Which gives the following compilation error:

app/services/ajax.ts(8,5): error TS2416: Property 'namespace' in type 'Ajax' is not assignable to the same property in base type '{ contentType: string; headers: undefined; host: undefined; namespace: undefined; trustedHosts: undefined; request<T = any>(url: string, options?: AJAXOptions): AJAXPromise<T>; raw<T = any>(url: string, options?: AJAXOptions): AJAXPromise<...>; ... 27 more ...; normalizeErrorResponse(_status: number, _headers: Heade...'.
  Type 'string' is not assignable to type 'undefined'.

In the source the value of namespace is defined as undefined:

namespace: undefined,

This means it doesn't accept a value of string. To allow this, the type definition needs to look like this:

namespace: undefined | string,

There's a few additional instances like this (headers, host, trustedHosts) and possibly more.

@alexlafroscia
Copy link
Collaborator

Thanks for the report! I'll definitely look into this

@alexlafroscia alexlafroscia added the Bug Issue relates to a problem in the `ember-ajax` codebase label Oct 10, 2018
@alexlafroscia
Copy link
Collaborator

@pepke41 and I have been looking into this some and plan to fix it in a coming release.

The namespace: undefined | string, example you provided didn't work from us.

https://www.typescriptlang.org/play/index.html#src=const%20object%20%3D%20%7B%0D%0A%20%20%20%20foo%3A%20undefined%20%7C%20string%0D%0A%7D%3B

But I think we can use the unknown type (or maybe the as keyword?) to solve the problem instead.

@phil294
Copy link

phil294 commented Mar 29, 2019

This looks fixed. Isnt it?

@James-Byrne
Copy link

James-Byrne commented Jun 20, 2019

Just ran into this issue today. I'm currently getting the same error with headers & trustedHosts. I'm defining them like:

@computed()
get trustedHosts() {
  return [/\.somedomain/];
}

get headers() {
  const headers: Headers = {
    Accept: 'application/vnd.api+json',
    'Content-Type': 'application/vnd.api+json',
  };
  
  // ...

  return headers;
}

I've added the different return types combinations above and my own types but I still see the same error.

@Bouke
Copy link
Author

Bouke commented Jun 8, 2020

This has been fixed in 2e2b0c5, but hasn't been released yet.

@jonchay
Copy link

jonchay commented Jan 14, 2021

I'm experiencing this problem as well. Is it possible to publish the fix?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Issue relates to a problem in the `ember-ajax` codebase
Projects
None yet
Development

No branches or pull requests

5 participants