Skip to content

Change in declaration file resolution in 2.9 when package.json doesn't specify types #24556

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

Closed
seepel opened this issue Jun 1, 2018 · 6 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@seepel
Copy link

seepel commented Jun 1, 2018

TypeScript Version: 2.9.1 and 3.0.0-dev.201xxxxx

Typescript 2.9 seems to have changed how type declaration files are resolved when package.json does not contain a types key, and there is a declaration file next to the main script.

For example the Semantic UI React project (https://github.com/Semantic-Org/Semantic-UI-React) has the following (relevant) file structure:

* package.json
* index.d.ts
* dist
  * commonjs
    * index.js
    * index.d.ts

package.json specifies the main script to be dist/commonjs/index.js and does not specify a types key.

In typescript 2.8 when importing from this module the type declaration was resolved to node_modules/semantic-ui-react/index.d.ts.
In typescript 2.9 the type declaration is resolved to node_modules/semantic-ui-react/dist/commonjs/index.d.ts

I definitely understand how one could argue either way, but wanted to make sure that this surfaced somewhere in case it was unintentional, and perhaps suggest that if it was intentional this change be made in typescript 3.0 as it is a breaking change?

Search Terms:

  • types
  • package.json
  • declaration file
  • resolve
  • resolution

Code

import React from "react"
import { Container } from "semantic-ui-react"


export default function() { return <Container/> }

Expected behavior:
Compiles with no errors

Actual behavior:

index.tsx:2:10 - error TS2305: Module '"/Users/sean/src/test/node_modules/semantic-ui-react/dist/commonjs/index"' has no exported member 'Container'.

2 import { Container } from "semantic-ui-react"
           ~~~~~~~~~

Playground Link:
No playground link as this depends on node_modules.

Related Issues:
I did not find any related issues

@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 1, 2018
@mhegazy mhegazy assigned ghost Jun 1, 2018
@mhegazy mhegazy added this to the TypeScript 2.9.2 milestone Jun 1, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jun 1, 2018

@Andy-MS can you take a look for 2.9.2 please

@ghost
Copy link

ghost commented Jun 1, 2018

Looks like this was changed intentionally in #24112.

@seepel
Copy link
Author

seepel commented Jun 1, 2018

Thanks for coming back on this so quickly. I saw that semantic-ui-react actually added the types key to package.json last night, though it is not published to npm yet.

A work around that I've found seems to work, is to copy the package's index.d.ts file into a directory under my baseUrl such as ./my-type-modules/semantic-ui-react/index.d.ts and set paths for the package to resolve there

{
  "baseUrl": ".",
  "paths": {
    "semantic-ui-react": ["my-type-modules/semantic-ui-react/index.d.ts"]
  }
}

Does that seem like the best work around for this issue?

@mhegazy mhegazy assigned sheetalkamat and unassigned ghost Jun 1, 2018
@gomezhyuuga
Copy link

Same issue here ✋
I had to go back to 2.8.3

@sheetalkamat
Copy link
Member

@mhegazy #24112 was intentional change to give better typing when types field is missing in the package to use "main" field over other default locations.

@AviVahl
Copy link

AviVahl commented Jun 7, 2018

@sheetalkamat That's a nice feature. 👍

Might be worth a separate ticket, but what about the "module" field, which points to the "esnext" version being published (and is picked over "main" by webpack/rollup)?

I personally publish isomorphic libraries as both cjs and esm. Here's an example: https://github.com/wixplosives/sample-monorepo/blob/master/packages/components/package.json

I do use the "types" field, but if one forgets to hook that up, would be nice if TypeScript could also pick up "module" over "main".

EDIT: nvmind, found #21423 :)

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Jun 7, 2018
@mhegazy mhegazy closed this as completed Jun 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants