Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Type of implicit class property is any. #645

Open
bowenni opened this issue Dec 29, 2017 · 8 comments
Open

Type of implicit class property is any. #645

bowenni opened this issue Dec 29, 2017 · 8 comments
Labels

Comments

@bowenni
Copy link
Contributor

bowenni commented Dec 29, 2017

A very common pattern I saw from the recent migrations:

class A {
  /** @param {boolean}  p */
  constructor(p) {
    /** @private */
    this.p_ = p;
  }
}

This code is converted to:

class A {
  private p_: any;
  constructor(p: boolean) {
    this.p_ = p;
  }
}

Would it make sense to emit this instead?

class A {
  private p_: boolean;
  constructor(p: boolean) {
    this.p_ = p;
  }
}

The renaming the parameter property collapsing is left to TSLint.

@bowenni bowenni added the gents label Dec 29, 2017
@mprobst
Copy link
Contributor

mprobst commented Dec 29, 2017 via email

@evmar
Copy link
Contributor

evmar commented Jan 2, 2018

Clutz only sees the types that Closure sees.

I tried the below program, which suggests that Closure thinks this.p_ really is any.

class A {
  /** @param {boolean}  p */
  constructor(p) {
    /** @private */
    this.p_ = p;
  }

  foo() {
    this.p_ = 'hi';
  }
}

new A(true).foo();

@bowenni
Copy link
Contributor Author

bowenni commented Jan 2, 2018

Yes this.p_ is indeed any.

But we can refer the type from the constructor parameters. Since constructor(p: boolean) and this.p_ = p we can guess that this.p_ is also a boolean.

@dpurp
Copy link
Contributor

dpurp commented Jan 2, 2018 via email

@bowenni
Copy link
Contributor Author

bowenni commented Jan 2, 2018

Adding a @const makes it private readonly p_: any;. The type is still any, I think.

@evmar
Copy link
Contributor

evmar commented Jan 2, 2018

I guess, my point is if we start inferring types beyond what Closure does we're moving into the space of implementing our own impl of a type system. I wonder if we could instead get the user to fix their code to have types. For example, isn't there some noImplicitAny-equivalent flag for Closure code? We could ask users to turn that on before migration.

@dpurp
Copy link
Contributor

dpurp commented Jan 3, 2018

There are a few options with js-conformance, and they are suggested as part of the migration workflow. I suspect that most teams want to skip the pre-work and go straight to TypeScript, instead of fixing the errors in Closure-land.

@mprobst
Copy link
Contributor

mprobst commented Jan 3, 2018 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants