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

3.7.4 emits d.ts files incompatible as inputs to programs with noImplicitAny or strict flags #36216

Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@IgorMinar
Copy link

IgorMinar commented Jan 16, 2020

TypeScript Version: 3.7.4

Search Terms:
noImplicitAny, strict, d.ts, declaration, getter, get, private

Code

class PrivateClass {}

export class ExportedClass {
  private get _privateRef(): PrivateClass { return new PrivateClass() }
}

tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "rootDir": ".",
    "target": "es5",
    "types": [],
    "declaration": true
  }
}

Expected behavior:

produces d.ts which if compiled against in a program with strict mode on is compiled successfully, for example (note _privateRef(): any):

export declare class ExportedClass {
  private get _privateRef(): any;
}

Actual behavior:
get _privateRef() is emitted without any explicit type and is implicitly set to any which triggers compilation failure:

export declare class ExportedClass {
  private get _privateRef();
}

resulting in:

error TS7033: Property '_privateRef' implicitly has type 'any', because its get accessor lacks a return type annotation.

1 export declare class ExportedClass { private get _privateRef(); }

Playground Link:

Related Issues:

IgorMinar added a commit to IgorMinar/angular that referenced this issue Jan 16, 2020
Typescript 3.7 now emits d.ts files for getters differently than prior versions,
and there seems to be a bug in how it strips private types without replacing them
with explicit 'any' type. This then leads to compilation failures in projects compiled
against our packages that don't have skipLibCheck turned on but do have strict or
noImplicitAny check on.

I'm working around this by marking the affected getters as @internal and
adding a test to prevent future regressions.

I believe this is a TypeScript bug, and I filed a bug report:
microsoft/TypeScript#36216
@IgorMinar
Copy link
Author

IgorMinar commented Jan 16, 2020

Real world d.ts file that fails to compile: https://unpkg.com/browse/@angular/compiler@9.0.0-rc.9/src/output/abstract_emitter.d.ts#L23

You can test it by compiling a simple program:

setup:

yarn add @angular/compiler@9.0.0-rc.9

program:

import * as test from '@angular/compiler';
export default { test };

IgorMinar added a commit to IgorMinar/angular that referenced this issue Jan 16, 2020
Typescript 3.7 now emits d.ts files for getters differently than prior versions,
and there seems to be a bug in how it strips private types without replacing them
with explicit 'any' type. This then leads to compilation failures in projects compiled
against our packages that don't have skipLibCheck turned on but do have strict or
noImplicitAny check on.

I'm working around this by marking the affected getters as @internal and
adding a test to prevent future regressions.

I believe this is a TypeScript bug, and I filed a bug report:
microsoft/TypeScript#36216
@RyanCavanaugh
Copy link
Member

Ugh, this is awkward. We shouldn't have ever complained about the missing : any type annotation, and in fact we should disallow type annotations here at all -- private fields in .d.ts files just shouldn't have type annotations.

I am inclined to make a 3.7.6 to prevent this from turning into a long-term compat annoyance. Would that be sufficient for your setup?

@RyanCavanaugh
Copy link
Member

cc @sandersn in case he has any thoughts

@filipesilva
Copy link
Contributor

I think it would be sufficient for our setup, yes.

If doing a 3.7.6, we could also use #36098 since it affects other parts of our setup.

@ajafff
Copy link
Contributor

ajafff commented Jan 16, 2020

This should already be fixed with #33896 which according to the tags is included in v3.7.4

@filipesilva
Copy link
Contributor

@ajafff I think your fix addresses the problem for consumers on 3.7.4, is that correct?

In our case we have typings created with 3.7.4 that we also wish to be consumed by users on 3.6.

@ajafff
Copy link
Contributor

ajafff commented Jan 16, 2020

@filipesilva you are right. My PR removes this restriction from 3.7.4, it doesn't change the emitted declarations.

In our case we have typings created with 3.7.4 that we also wish to be consumed by users on 3.6.

So this is a duplicate of #36207 (and many more). The proposed solution is to use https://www.npmjs.com/package/downlevel-dts. This tool could probably add an explicit : any return type for get accessors when tareting 3.6.

@IgorMinar
Copy link
Author

I am inclined to make a 3.7.6 to prevent this from turning into a long-term compat annoyance. Would that be sufficient for your setup?

@RyanCavanaugh yes

matsko pushed a commit to angular/angular that referenced this issue Jan 16, 2020
…#34798)

Typescript 3.7 now emits d.ts files for getters differently than prior versions,
and there seems to be a bug in how it strips private types without replacing them
with explicit 'any' type. This then leads to compilation failures in projects compiled
against our packages that don't have skipLibCheck turned on but do have strict or
noImplicitAny check on.

I'm working around this by marking the affected getters as @internal and
adding a test to prevent future regressions.

I believe this is a TypeScript bug, and I filed a bug report:
microsoft/TypeScript#36216

PR Close #34798
matsko pushed a commit to angular/angular that referenced this issue Jan 16, 2020
…#34798)

Typescript 3.7 now emits d.ts files for getters differently than prior versions,
and there seems to be a bug in how it strips private types without replacing them
with explicit 'any' type. This then leads to compilation failures in projects compiled
against our packages that don't have skipLibCheck turned on but do have strict or
noImplicitAny check on.

I'm working around this by marking the affected getters as @internal and
adding a test to prevent future regressions.

I believe this is a TypeScript bug, and I filed a bug report:
microsoft/TypeScript#36216

PR Close #34798
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Jan 18, 2020
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.7.6 milestone Jan 18, 2020
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 18, 2020

So what's the new behavior? Always emit these with an unknown and/or any? Does 3.7.6 also not consider this a noImplicitAny error?

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 18, 2020

We could also make 3.6 not error on this instead because I think it's the only version that actually recognizes getters in a .d.ts

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 23, 2020

We're going to do the latter. Users who run into issues will be able to upgrade to 3.6.5 from ≤3.6.4.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Jan 23, 2020
@DanielRosenwasser
Copy link
Member

3.6.5 should be available now. Thanks for the the heads up on all this!

@DanielRosenwasser DanielRosenwasser removed the Domain: Declaration Emit The issue relates to the emission of d.ts files label Jan 23, 2020
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment