Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script-able types for better future #55249

Closed
5 tasks done
loynoir opened this issue Aug 3, 2023 · 4 comments
Closed
5 tasks done

script-able types for better future #55249

loynoir opened this issue Aug 3, 2023 · 4 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@loynoir
Copy link

loynoir commented Aug 3, 2023

Suggestion

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

  • script-able types

  • types provider

  • types of network import

  • types of json module

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

{
    "typesProvider": "@typesgen/providerImportId"
}

📃 Motivating Example

💻 Use Cases

  • script-able types for network import .ts

  • script-able types for const/non-const json module

  • script-able types for jsonschema

  • script-able types for network import https://cdn

  • script-able types for pnp-zip-like layer

  • script-able types for database

  • script-able types for webidl

  • script-able types for future, for example, const/non-const yaml module

export default async function provider(
    importMetaUrl: ImportMeta["url"]
): Promise<null | dtsAST> {
    if (
        importMetaUrl === "http://git-server-with-correct-mime-type/example.ts"
    ) {
        const value = await read(importMetaUrl);
        return userUtils.ts2dts(value) as dtsAST;
    }

    if (jsonId.test(importMetaUrl)) {
        const { value, asConst } = await jsonId.parse(importMetaUrl);
        return userUtils.jsondts(value, { asConst }) as dtsAST;
    }

    if (jsonSchemaId.test(importMetaUrl)) {
        const value = await read(importMetaUrl);
        return userUtils.jsonSchema2dts(value) as dtsAST;
    }

    if (networkId.test(importMetaUrl)) {
        const { name, version, isSkypack, isEsmsh } =
            networkId.parse(importMetaUrl);
        return userUtils.npmdts({ name, version }) as dtsAST;
    }

    if (pnpzipId.test(importMetaUrl)) {
        const { name, version } = pnpzipId.parse(importMetaUrl);
        return userUtils.npmdts({ name, version }) as dtsAST;
    }

    if (importMetaUrl.endsWith("database-types")) {
        const dbconfigs: DBConfig[] = CONFIG.dbconfigs;
        return userUtils.dbdts(dbconfigs) as dtsAST;
    }

    if (webidlId.test(importMetaUrl)) {
        const value = await read(importMetaUrl);
        return userUtils.webidl2dts(value) as dtsAST;
    }

    // future-ish features, for example, yaml module
    if (yamlId.test(importMetaUrl)) {
        const { value, asConst } = await yamlId.parse(importMetaUrl);
        return userUtils.yamldts(value, { asConst }) as dtsAST;
    }

    return null;
}

Else

  • Should provider be an async generator function, to inform typescript dts changes?
@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 3, 2023

Sounds like a duplicate of #41577 to me. Or #3136. Both found using your search terms.

@loynoir
Copy link
Author

loynoir commented Aug 3, 2023

Sounds like a duplicate of #41577 to me. Or #3136. Both found using your search terms.

I consider two as subset of this feat.

@loynoir
Copy link
Author

loynoir commented Aug 3, 2023

Thus, the solution should be implemented within user land, instead of within typescript.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Aug 3, 2023
@RyanCavanaugh
Copy link
Member

We already have a disk-persisted format for types and consider this to be the preferred solution in every case listed here.

Throwing a Turing-complete compile-time type generator process into the mix is opening an infinite can of worms of complexity that we won't ever be able to close. Regenerating all of this stuff on every build is poison for performance, opens up a very significant new security boundary, makes it impossible for users to debug what's going on, creates problems in editor experiences (e.g. how do you "view" this source code?), and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

3 participants