question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Continuing from https://github.com/Microsoft/TypeScript/issues/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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

2reactions
trusktrcommented, 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?

1reaction
trusktrcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common TypeScript module problems and how to solve them
If the compiler finds a package.json file, then it will check whether that file contains a types property that points to a typings...
Read more >
Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
How to get rid of the warning .ts file is part of the TypeScript ...
It turned out that you need to remove this line from "include" "src/**/*.ts" from tsconfig.app.json and only keep entry points in files ......
Read more >
package.json - npm Docs
It's assumed that it's js, since you're writing a package.json file, ... The main field is a module ID that is the primary...
Read more >
Angular package format
json, compiled source code, typescript definition files, source maps, metadata, etc. The package is installed with npm install @angular/core . Symbollink. A ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found