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

"types" field in package.json pointing to a .ts file in node_modules results in the file being compiled and type checked #35744

Open
trusktr opened this issue Dec 18, 2019 · 7 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@trusktr
Copy link
Contributor

trusktr commented Dec 18, 2019

Continuing from #22228

cc @evil-shrike

I have this issue.

@mhegazy said

the lib has either "types": "index.ts" or a .ts file at its root. which is wrong. a library should not expose its sources, only its declarations.

But features like inferred return types (f.e. when making class-factory mixins) are not compilable to declaration files, resulting in errors like

error TS4025: Exported variable 'html' has or is using private name 'htmlBind'.
error TS4031: Public property '_currentArea' of exported class has or is using private name 'AreaInternal'.
error TS4055: Return type of public method from exported class has or is using private name 'PartHelper'.
error TS4073: Parameter 'partHelper' of public method from exported class has or is using private name 'PartHelper'.
error TS4078: Parameter 'options' of exported function has or is using private name 'ExtendOptions'.

A .d.ts does not have expressions.. it represents the shape of the API.

Not entirely true.

As far as I know, the only way to use features (that declaration files don't support) in downstream projects is to get types directly from .ts source files. This makes the need to point types to .ts source files a valid use case.


This is what I think should happen:

If "types" points to a .ts file, and "main" points to a .js file, then the compiler should use the .ts file only for type definitions and not compile or type-check the code.

"main" can serve as a guide to telling the compiler whether it should compile sources, or read js files. "types" should be for... specifying the source of types.

Unless I missed it, there's no other way to include types for features that aren't representable in declaration files.

Why is it that declaration features don't match source features? It seems that an important goal should be for declaration features to always have the capability of matching source features.

@trusktr trusktr changed the title Discovering .ts files under node_modules resulting in them being compiled and type checked "types" field in package.json pointing to a .ts file in node_modules results in the file being compiled and type checked Dec 18, 2019
@trusktr
Copy link
Contributor Author

trusktr commented Dec 18, 2019

By the way, when making things like class-factory mixins and then trying to emit declaration files we get problems like described in #23110.

@trusktr
Copy link
Contributor Author

trusktr commented Dec 18, 2019

If we can't have implicit return types, etc, in declaration files, and we shouldn't point "types" to .ts source files, then what should we do?

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Dec 18, 2019
@trusktr
Copy link
Contributor Author

trusktr commented Dec 20, 2019

How can all features of TS be somehow supported in declaration files?

Maybe implicit return types could look like this:

export declare function AwesomeMixin<T extends Constructor>(Base: T) {
  type Foo = SomeOtherType<T> // only types allowed in here.
  
  // only special return statements allowed, or something.
  return declare class Awesome extends Base {
    method(): Foo
  }
}

where there's no implementation. It looks like an implementation being inside the {}, but maybe it is limited to containing only types and return types, or something similar. We'd just need to define the rules.

@trusktr
Copy link
Contributor Author

trusktr commented Jun 6, 2020

Feature request: allow types field of package.json to point to a source .ts file, then prevent the compiler from type checking the internals, just use those files for type information as it relates to consumption. Should I open a new issue for it specifically?

@trusktr
Copy link
Contributor Author

trusktr commented Nov 3, 2020

@sheetalkamat You recently merged #32028. Does that issue mean we can close this one? Can you also check my comments in that PR?

@trusktr
Copy link
Contributor Author

trusktr commented Nov 3, 2020

Based on #40431 (comment) by @RyanCavanaugh, that PR #32028 compiles to .d.ts files in memory, then relies on those virtual declaration files.

If so, I think that can be the solution for this issue if it isn't already.

Basically tsc would look at types in package.json then generate the .d.ts for that in memory instead of using source files. Wdyt?

@trusktr
Copy link
Contributor Author

trusktr commented Apr 19, 2021

Basically tsc would look at types in package.json then generate the .d.ts for that in memory instead of using source files. Wdyt?

There's only one problem with this idea: all the issues listed in #35822 will happen, and cause errors regarding "private name", etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants