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

generating interfaces instead of implenetations #462

Closed
Coobaha opened this issue Sep 11, 2020 · 8 comments
Closed

generating interfaces instead of implenetations #462

Coobaha opened this issue Sep 11, 2020 · 8 comments

Comments

@Coobaha
Copy link
Contributor

Coobaha commented Sep 11, 2020

Hi @cristianoc

I noticed that most of the time in our codebase we are actually interested more in generating interface files (.d.ts) instead of .gen.tsx. For this we have tsc that generates .gen.js and .d.gen.ts from .gen.ts.

I think it will be amazing if genType will have a new mode that will print .gen.js and .d.gen.ts right away. So that we can just ship reason packages ready to be consumed in any js lib

@cristianoc
Copy link
Collaborator

@Coobaha interested in learning more how these look like. Would you add a couple of examples of .gen.js and .d.gen.ts files from simple examples?

@Coobaha
Copy link
Contributor Author

Coobaha commented Sep 12, 2020

how I see this mode:

  • outputs js (current untyped mode)
  • outputs typescript interface files (similar to typescript printer, but slightly different syntax and only types ofc)

I print interface files from ts code with

tsc --emitDeclarationOnly --declaration

We have two steps build for modules that will be consumed as npm packages:

  1. output js+tsx with rescript+gentype
  2. produce js only output with tsc by transpiling .gen.tsx to .gen.js + .d.gen.ts

example of definitons from gen.tsx https://gist.github.com/Coobaha/d92f549d5d72c54918712ca8c9244e2c

@cristianoc
Copy link
Collaborator

cristianoc commented Sep 15, 2020

Noting down a couple of quick observations:

  • There's the question of what do do when importing from TS, the checking code is emitted in the .gen.tsx file.
  • Interface files .d.gen.ts seem to behave more liberally. E.g. let you import a type from a non-existing module without giving errors. In general, they might provide fewer sanity checks for the generated code.

@johnridesabike
Copy link

I would be interested in this too. I would ideally like to keep a package.json that looks like this:

{
  "exports": {
    ".": {
      "import": "./lib/es6/src/Main.mjs",
      "require": "./lib/js/src/Main.js"
    },
    "./lib/*": "./lib/*",
    "./src/*": "./src/*"
  },
  "main": "lib/js/src/Main.js",
  "module": "lib/es6/src/Main.mjs",
  "types": "types/Main.d.ts"
}

ReScript can manage everything except for the types. To generate the types, I would currently need to compile *.gen.tsx files and then build the *.d.ts files from those. Currently, that also means setting "in-source": true, "suffix": ".bs.js", configuring tsc, and generating a lot of files that are just for generating the final *.d.ts. If this process could be streamlined, that would great.

@gustavopch
Copy link

gustavopch commented May 2, 2021

Would TypeScript understand .d.gen.ts? I think .gen.d.ts would be the right extension. Or even .bs.d.ts so that it matches the original JavaScript file generated by the compiler.

@gustavopch
Copy link

gustavopch commented May 2, 2021

In case anyone is struggling with this, I was able to generate .d.ts files by hooking the following script into js-post-build option in bsconfig.json:

#!/usr/bin/env bash

bsJsPath="$1"
bsDTsPath="${bsJsPath/bs\.js/bs\.d\.ts}"
genTsxPath="${bsJsPath/bs\.js/gen\.tsx}"
genDTsPath="${bsJsPath/bs\.js/gen\.d\.ts}"

# Generate .gen.d.ts file (note: when tsc receives a path, it'll ignore tsconfig.json)
tsc --skipLibCheck --declaration --emitDeclarationOnly "$(readlink -f "$path")"

# Rename from .gen.d.ts to .bs.d.ts so that someone importing the .bs.js file from
# vanilla JS also gets better IntelliSense
mv -f "$genDTsPath" "$bsDTsPath"

@pd4d10
Copy link

pd4d10 commented Dec 11, 2022

Encountered this issue +1. The implementation in gen.tsx file seems not necessary since we already have bs.js shipped.

Is there any plan? I'm glad to help if the objective is clear

@ryyppy
Copy link
Member

ryyppy commented Nov 13, 2023

This repository is being archived. If you feel like the issue still relevant, please re-create it in the compiler repo. Thanks!

@ryyppy ryyppy closed this as completed Nov 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants