Skip to content

Typescript Quick Start example has type errors #2228

Closed

Description

@SephReed

Steps to reproduce

  1. Npm install all the listed imports:
// these four, npm install each
import feathers from '@feathersjs/feathers';
import '@feathersjs/transport-commons';
import express from '@feathersjs/express';
import socketio from '@feathersjs/socketio';
  1. Copy /paste the given code

Expected behavior

There should be no type errors

Actual behavior

Tons of type errors
Screen Shot 2021-02-18 at 9 23 44 PM

Activity

  1. SephReed commented on Feb 19, 2021

    @SephReed
    Author

    It's this same issue I ran into a while ago. (#2120)

    tsconfig.json MUST have "allowSyntheticDefaultImports": true,

    This really should be put in big giant bold letters somewhere on the getting started page. It literally breaks the entire thing in typescript if you don't have that one tsconfig setting.

  2. SephReed commented on Feb 19, 2021

    @SephReed
    Author
  3. daffl commented on Feb 19, 2021

    @daffl
    Member

    This happens when you do not follow the first TypeScript step in the guide, that specifies to use tsc --init --target es2018:

    Screen Shot 2021-02-18 at 8 36 00 PM

  4. SephReed commented on Feb 19, 2021

    @SephReed
    Author

    Ah. I've done the setup for a typescript project so many times that I did all the "create an npm project and install typescript" steps in the normal way that works for every other project.

    I see that you're correct in that following these steps exactly would work. Nothing about the code snippet above implies the importance of "allowSyntheticDefaultImports" coming from the line npm init --yes.

    I don't see any reason why such important configuration should be so obfuscated.

  5. daffl commented on Feb 19, 2021

    @daffl
    Member

    I don't remember what difference the ES2018 setting makes (imho it should be the default) but I think it had something to do with how imports are handled (it also doesn't transpile async/await and things like that since it's available in all supported Node versions).

    Something I've been hoping to get some help with for a while is to store the last language tab (JS/TS) you selected (or have a language choose) in localstorage and always select that language by default. I think that would make going through the guide in that regard a little more clear.

  6. SephReed commented on Feb 21, 2021

    @SephReed
    Author

    That would be a nice feature for sure.

    I seems you're using "vuepress" for your rendering, which is something I've never used before.

    But honestly, for a feature like this, you could get away with just including a vanilla script. Here ya go:

    This script will:

    • use mutation observers to watch for nodes added to the page
    • find any added language tabs
      • modify them so on click they'll update local storage, and all other tabs
      • simulate a click() on the preferred one
    let langSwitchIsSetup = false;
    function setupLangSwitch() {
      if (langSwitchIsSetup) { return; }
      langSwitchIsSetup = true;
    
      const observer = new MutationObserver((muts) => {
        muts.forEach((mut) => {
          mut.addedNodes.forEach((node) => {
            if (node instanceof HTMLElement) { 
              initLangTabs(node);
              clickPreferredLangTabs(node);
            }
          })
        })
      });
      observer.observe(document.body, {
        subtree: true,
        childList: true,
      });
    
      initLangTabs(document.body);
      clickPreferredLangTabs(document.body);
    }
    
    function initLangTabs(root: HTMLElement) {
      ["javascript, typescript"].forEach((lang) => {
        root.querySelectorAll(`*[aria-controls="#${lang}"]`).forEach((tab) => {
          if (tab.__langSetupTouched) { return; }
          tab.addEventListener("click", () => {
            if (lang === localStorage.getItem("preferredLang")) { return; }
            localStorage.setItem("preferredLang", lang);
            clickPreferredLangTabs(document.body);
          });
          tab.__langSetupTouched = true;
        });
      });
    }
    
    function clickPreferredLangTabs(root: HTMLElement) {
      const lang = localStorage.getItem("preferredLang") || "javascript";
      root.querySelectorAll(`*[aria-controls="#${lang}"]`).forEach((node) => {
        (node instanceof HTMLElement) && node.click();
      })
    }
    

    It's not tested.


    Otherwise, you'll have to hook into the renderer, and that's a whole framework I've yet to learn.

  7. mrobst commented on Feb 25, 2021

    @mrobst
    Contributor

    I like the idea of the language selector. Will look into vuepress (at least add it to my backlog 😏)

  8. SephReed commented on Feb 26, 2021

    @SephReed
    Author

    Would be nice.

    Also, I still think that telling Typescript users that they absolutely must include "allowSyntheticDefaultImports": true in their tsconfig is important. I would be surprised if I'm the only one who sets up typescript projects from memory and has pre-existing tsconfig files they really like.

    feathersjs-ecosystem/docs#1518

  9. daffl commented on Feb 26, 2021

    @daffl
    Member

    As I mentioned in #2228 (comment) before, the correct option is to set the compilation target to ES2018 which the documentation already mentions and the generator does automatically.

  10. SephReed commented on Mar 3, 2021

    @SephReed
    Author

    I have my target set to ESNext which works. And expecting users to only ever approach Feathers integration in this one specific way seems ludicrous.

    Is there any documentation anywhere which explains how to integrate Feathers into an existing project? I couldn't find any.

  11. daffl commented on Mar 3, 2021

    @daffl
    Member

    I'm not sure what exactly you are looking for. The current version is supposed to work with NodeJS 12 and up which natively supports ES2018 and later so setting that as your transpiler target as documented and mentioned several times now should not cause any other problems. In fact it will probably make things smoother by giving you better stacktraces and performance by relying on built-in async/await instead of pointlessly transpiling it to nested generator functions.

  12. SephReed commented on Mar 10, 2021

    @SephReed
    Author

    The target: ESAnything property has nothing to do with "allowSyntheticDefaultImports": true. They are two separate properties.

    When you run tsc --init --target es2018 it just so happens that the allowSyntheticDefaultImports value gets set.

    But if (like me) you copy a tsconfig.json that you like from some other project, there's a good chance it won't have the very important "allowSyntheticDefaultImports": true property.

    Nothing about the documentation makes it clear that this would be an issue, but feathers-js won't transpile without it.

    Further, if you had a project already started (like me), there's also very good chance that tsconfig.json will be missing allowSyntheticDefaultImports.

  13. added this to the v5 (Dove) milestone on Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions