microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.75k stars 12.46k forks source link

In certain cases TS 2.9 outputs incorrect definitions (.d.ts) #25511

Closed d4rkr00t closed 6 years ago

d4rkr00t commented 6 years ago

TypeScript Version: 2.9.2 and 3.0.0-dev.201xxxxx

Search Terms: relaxed, declarations

Expected behavior: Definitions have correct import statements.

Actual behavior:

In certain cases e.g. monorepo structure where dependencies are linked inside packages from the root node_modules folder (you can read about that more here: bolt) output .d.ts files contain pretty weird paths...

Code: https://github.com/d4rkr00t/tsc29-test-relaxed-resolve

// packages/package-a/src/index.ts
import { css } from 'styled-components';

export const styles = css`
    display: table;
`;

Outputs definition file:

// packages/package-a/dist/index.d.ts
export declare const styles: import("../../../../../../../../Users/username/tsc29-test-relaxed-resolve/packages/package-a/node_modules/styled-components").InterpolationValue[];

I've also tried that with TS 3.0 it's a little bit better, first file gets correct deffinitions:

// packages/package-a/dist/index.d.ts
export declare const styles: import("styled-components").InterpolationValue[];

But another module that imports the previous one still gets wrong definitions (even though, they look sort of reasonable, but probably the fact that package-a is inside node_modules should indicate that this is a module, i'm not sure if it's fair to assume that :) ):

export declare function getStyles(): import("../../package-a/node_modules/styled-components/typings/styled-components").InterpolationValue[];

In version below 2.9 we had to import those things manually and it used to work, but now we can't do it manually anymore as it has no difference...

alex3165 commented 6 years ago

Having the same issue with a monorepo like structure too using oao and with Typescript 2.9.2

jaredpalmer commented 6 years ago

Formik has this issue and I'm not sure how to proceed.

import * as React from 'react';
import { FormikContext } from './types';
export declare const FormikProvider: React.ComponentClass<import("../../../../../../../Users/jared/workspace/github/jaredpalmer/formik/node_modules/create-react-context").ProviderProps<FormikContext<any>>>, FormikConsumer: React.ComponentClass<import("../../../../../../../Users/jared/workspace/github/jaredpalmer/formik/node_modules/create-react-context").ConsumerProps<FormikContext<any>>>;
export declare function connect<OuterProps, Values = {}>(Comp: React.ComponentType<OuterProps & {
    formik: FormikContext<Values>;
}>): React.ComponentClass<OuterProps> & {
    WrappedComponent: React.ComponentClass<OuterProps & {
        formik: FormikContext<Values>;
    }>;
};

https://github.com/jaredpalmer/formik/issues/720

jaredpalmer commented 6 years ago

Update: I bumped to v3.0.0-dev and this has been fixed. New output looks like:

import * as React from 'react';
import { FormikContext } from './types';
export declare const FormikProvider: React.ComponentClass<import("create-react-context").ProviderProps<FormikContext<any>>>, FormikConsumer: React.ComponentClass<import("create-react-context").ConsumerProps<FormikContext<any>>>;
export declare function connect<OuterProps, Values = {}>(Comp: React.ComponentType<OuterProps & {
    formik: FormikContext<Values>;
}>): React.ComponentClass<OuterProps> & {
    WrappedComponent: React.ComponentClass<OuterProps & {
        formik: FormikContext<Values>;
    }>;
};
d4rkr00t commented 6 years ago

@jaredpalmer Yeah, as i mentioned before TS@3.0 solves some of the issue, but it doesn't solve it completely for us :( I should actually try new monorepo support in 3.0 (a.k.a references) and see how it goes...

d4rkr00t commented 6 years ago

I just tried references, and it didn't help...

jaredpalmer commented 6 years ago

More specifically, installing today's release worked:

"typescript": "^3.0.0-dev.20180711",
n8agrin commented 6 years ago

@jaredpalmer I was experiencing similar issues with StyledComponents and today's release did fix my problems. Thank you!

drew-y commented 6 years ago

This is happening to me in a non-monorepo setup with typescript 2.9.2.

This function:

export const robotError = (code: number, trace?: Error) => errors.error(code, trace);

Is converted to this declaration:

export declare const robotError: (code: number, trace?: Error | undefined) => import("../../../../../../../Users/drew/projects/versabuilt/drivers/robot/node_modules/@vb/error/dist/definitions").VBError;

I think it must be implicit return type causing the issue.

weswigham commented 6 years ago

The issue most people here are harping on should have been fixed by #25364 and #25364. There's some comments in this threads stating that recent releases have worked for people in this thread - anyone here still having issues should try the latest 3.0 nightly and open a new issue (ideally with detailed repro steps, including full filesystem paths of the involved files) if it still reproduces under some set of circumstances for you.