Code Monkey home page Code Monkey logo

Comments (18)

xg-wang avatar xg-wang commented on August 15, 2024 2

image

from ember-fetch.

chriskrycho avatar chriskrycho commented on August 15, 2024 1

from ember-fetch.

chriskrycho avatar chriskrycho commented on August 15, 2024

Ugh. I was hoping that fetch would be an actual npm package, but it's whatwg-fetch, which isn't even a little bit better than ember-fetch. I agree that the best option here is exporting from ember-fetch instead, since the real npm fetch package is something totally different.

Your alternative does bring up an interesting point I'd like to think about in other packages as makes sense: if there's a good reason to, we can presumably teach people how to update the tsconfig.json as part of the addon installation process. That should certainly be a last-resort option, though.

from ember-fetch.

toranb avatar toranb commented on August 15, 2024

I was actually curious about the (most) correct path forward myself and thought it might be worth asking @DanielRosenwasser to chime in :)

warning and type defs are below for quick reference

A default export can only be used in an ECMAScript-style module.

export = fetch;

declare namespace fetch {
  export default function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}

This is how we import it in our ember applications

import fetch from 'fetch';

from ember-fetch.

DanielRosenwasser avatar DanielRosenwasser commented on August 15, 2024

A default export can only be used in an ECMAScript-style module.

Right now by default, TypeScript assumes the mapping between ES modules and CJS modules to be 1:1 (the namespace record and the imported object are treated the same). To get around that, you can use --allowSyntheticDefaultImports.

You can work around this with an explicit path mapping in tsconfig.json, but it's not conventional.

An alternative idea is to add

    "fetch": "ember-cli/ember-fetch"

which will get the current state of ember-fetch from this GitHub repo as a dependency

from ember-fetch.

toranb avatar toranb commented on August 15, 2024

@DanielRosenwasser thanks for the detailed reply! Reading this it would appear the synthetic option is most appropriate. Any downside to using this option generally speaking?

from ember-fetch.

toranb avatar toranb commented on August 15, 2024

@chriskrycho @dwickern I just upgraded to typescript v2.6 and this doesn't seem like an issue (anymore?). Here is a repo you can build to confirm it works/ or doesn't for reference

https://github.com/toranb/ember-redux-yelp/commits/branches/seamlessAndTypeScript

Is anyone else having issues w/ TypeScript v2.4/2.5 or 2.6 ?

from ember-fetch.

theseyi avatar theseyi commented on August 15, 2024

Still have the same issue with ts2.6 without using a parallel to your solution above, vis-a-viz an ambient module declaration @toranb

from ember-fetch.

lstrzebinczyk avatar lstrzebinczyk commented on August 15, 2024

We seem to still have this issue with typescript 2.6.2.

from ember-fetch.

chriskrycho avatar chriskrycho commented on August 15, 2024

I worked around it long since by doing "fetch": ["node_modules/ember-fetch/index.d.ts"], in my tsconfig.json, so at present that's the thing to do.

I don't agree with @toranb that the synthetic option is the most appropriate. I think it's actually more correct to have the export and import behave correctly at the package level, and then have the types map to that.

from ember-fetch.

lstrzebinczyk avatar lstrzebinczyk commented on August 15, 2024

I worked around it by including the types in my own code, but It should probably "just work" when including the package.

from ember-fetch.

DanielRosenwasser avatar DanielRosenwasser commented on August 15, 2024

I still kind of think that adding a package.json devDependency of

    "fetch": "ember-cli/ember-fetch"

might be the easiest thing.

from ember-fetch.

toranb avatar toranb commented on August 15, 2024

Just a quick note here - happy to pass the torch to others who legit know TypeScript :)

When I originally wrote these typedefs I was heads down with TypeScript but these days I don't actually use it (yet) and as a result don't know what is best for library/addon authors. If what @chriskrycho is suggesting seems like the best path forward please feel free to make a move without my involvement. I honestly just want to spin up an ember app (with TypeScript) at some point and get no errors when I turn on strict mode (using libraries like ember-fetch)

from ember-fetch.

nlfurniss avatar nlfurniss commented on August 15, 2024

Believe this issue should be solved now that TS support has been added

from ember-fetch.

theseyi avatar theseyi commented on August 15, 2024

I believe @xg-wang β€˜s PR has been merged but not released. So not sure that it has been β€œadded”. I still had this issue this week, Friday, running the latest released version of ember-fetch.

from ember-fetch.

theseyi avatar theseyi commented on August 15, 2024

@nlfurniss

from ember-fetch.

xg-wang avatar xg-wang commented on August 15, 2024

Sorry ember-fetch test is passing, but submitting to npm blocked by typed-ember/ember-cli-typescript#566, open PR DefinitelyTyped/DefinitelyTyped#33518

from ember-fetch.

theseyi avatar theseyi commented on August 15, 2024

Always a nice feeling to get rid of workarounds :)

from ember-fetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.