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

Better array and object inferred types with checkJs and strict / noImplicitAny #16137

Closed
cyrilletuzi opened this issue May 30, 2017 · 3 comments
Assignees
Labels
Suggestion An idea for TypeScript

Comments

@cyrilletuzi
Copy link

cyrilletuzi commented May 30, 2017

TypeScript Version: 2.3.3

Code

In a JavaScript file (not TypeScript), with the new checkJs option enabled :

let list = [`value 1`, `value 2`, 10];

let user = {
  name: `John`,
  age: 20
};

user.newDynamicProperty = `test`;

Actual behavior:

In normal mode, no errors are raised, and it's OK as it's the normal JS behavior.

In strict or noImplicitAny mode, no errors are raised either, despite the fact that the inferred type for user is : { [x: string]: any; name: string; age: number; }. For the array, it's (string | number)[] but in the purpose of error checking it's like any[].

Expected behavior:

In strict or noImplicitAny mode, errors should be raised.

It seems important to me for 2 reasons :

  • with the current behavior, manual type via JSDoc is required to have a correct error checking like in TS, so it requires more work than in TS (usually it should be less) and despite the fact that TS already knows how to manage that ;
  • it's specially painful for objects as we have no interfaces in JS, and as multi-lines
    @type or @param JSDoc comments are not supported by TS yet (see Intellisense for object properties defined in multi-line JSDoc comments #11597).
@cyrilletuzi cyrilletuzi changed the title Better array and object inferred types with jscheck and strict / noImplicitAny Better array and object inferred types with checkJs and strict / noImplicitAny May 30, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 30, 2017

in a .js file there are few places where any is injected. this is one of them, but there are other ones. --noImplicitAny really is not honored here correctly. as a matter of fact we have an issue to disable reporting --noImplicitany errors in a .js files, since they are incomplete at best (see #14617).

The assumption here is js file checking with checkJs is a loose version of checking; our mental model of this feature is that there is a spectrum of strictness, where:
checking .js file < checking .ts file < checking .ts file with --strict.

Just for curiosity; have you considered moving your files to .ts?

@cyrilletuzi
Copy link
Author

I use Angular so I'm used to TypeScript and I love it, but I'm very interested with the new checkJs for some other projects where no framework and no transpilation is needed, to stay in native JS but using checkJs to have the power of TS for type checking.

After more investigation, array behavior is quite OK : you can mix values of different types on array creation, but once the type is inferred, checkJs will raise errors if I don't respect it.

It's OK for objects with @typedef JSDoc, but it's quite a heavy solution for some small case. As it's just because [x: string]: any; is added to objects type, wouldn't it possible to just change that in strict mode, like does TS ?

I'll post another message in the other topic you mention.

@mhegazy mhegazy added Suggestion An idea for TypeScript Salsa labels Aug 23, 2017
@que-etc
Copy link

que-etc commented Feb 20, 2018

@cyrilletuzi

May I ask you why you did you decide to close this issue?

Inferred type of an object still has [x: string] any; and I can't seem to find a workaround for it, except for providing a @typedef for an every object being created.

It would be so much helpful if TypeScript raised errors when it encounters an undefined property, just like it happens with ts files:

const sample = {
    DEFINED_PROPERTY: 'FOO'
};

sample.UNDEFINED_PROPERTY.indexOf('BAR');

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants