TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

"Stricter" TypeScript

Open RyanCavanaugh opened this issue 12 years ago • 40 comments

This a meta-bug for tracking a set of things that we could address with a compiler flag that tightens certain aspects of TypeScript that users generally perceive as too loose.

Will add to this list as appropriate

  • #228: Enforce expressions in return statements in type-annotated functions
  • #191: Don't allow mismatched constant string overloads to be selected due to other parameters
  • #21: Disallow use before definition (for types? for everything?)
  • ~~#360: Error when generic type inference produces ex nihilo {}~~ - now a default

Contentious issues that have been mentioned:

Done!

  • #391: Detecting accidental surplus properties

Not happening:

  • ~~#222: Function argument bivariance~~
    • We would want some strong motivating examples here; this behavior is desired in a lot of scenarios and is less "surprising" than others.
    • This isn't a plausible design path. See comments here https://github.com/Microsoft/TypeScript/issues/6102#issuecomment-164629334

Note: This is not a "fork the language" flag. The type system itself would be unchanged under this flag; it would simply change some operations from being allowed to being errors. For example, we would not change the order of overload resolution, or change the type of null, because that would have non-local effects and everyone would have to agree on whether or not the flag was on.

RyanCavanaugh avatar Jul 28 '14 20:07 RyanCavanaugh

Does it make sense for noImplicitAny to be rolled in as part of this stricter TypeScript? Or is there value keeping it separate?

johnnyreilly avatar Jul 28 '14 20:07 johnnyreilly

I'd like to hear feedback on that one -- would anyone want --strict without --noImplicitAny ?

RyanCavanaugh avatar Jul 28 '14 21:07 RyanCavanaugh

I think --strict should include --noImplicitAny.

johnnyreilly avatar Jul 29 '14 04:07 johnnyreilly

I think --strict should include --noImplicitAny.

:+1:

basarat avatar Jul 29 '14 04:07 basarat

It'd be nice to have a flag to tighten the type compatibility rules (e.g. #222)

vladimir-i avatar Jul 29 '14 04:07 vladimir-i

I think --strict should include --noImplicitAny. :+1:

knazeri avatar Jul 29 '14 05:07 knazeri

This is a great meta-issue. My preference is for all these features (including noImplicitAny) to be included in the default compilation (i.e. no flags).

We could have a flag that people can use to relax the rules, for example --lenient or --dontCatchErrors (that's a joke, btw).

A --strict flag might be confused with "use strict".

NoelAbrahams avatar Jul 29 '14 15:07 NoelAbrahams

A --strict flag might be confused with "use strict".

that is what I thought when I first looked at the title

basarat avatar Jul 29 '14 17:07 basarat

Yeah it may be the case that we need to use a different term than 'strict' to minimize confusion but it's ultimately a small issue that's easy to change at any time before the feature goes into a final release.

danquirk avatar Jul 29 '14 18:07 danquirk

@NoelAbrahams - it'd need to be behind a flag to not break backward compatibility with code that's building against the current 1.0 compiler.

Not that I want to start a woodshed about naming, but I :+1: that "strict" is too overloaded, especially in the JS world.

sophiajt avatar Jul 31 '14 16:07 sophiajt

It's too late to make noImplicitAny the default; this would break too many people.

I agree with the sentiment that strict is overloaded in JS and is probably not the best name for the flag should it be implemented. Ideas on that front?

RyanCavanaugh avatar Aug 05 '14 04:08 RyanCavanaugh

It's too late to make noImplicitAny the default; this would break too many people.

:+1:

Ideas on that front?

--safer or --loud

basarat avatar Aug 05 '14 06:08 basarat

How about --hardcore or --optionExplicit? :smile:

In all seriousness I think --safe works well as would --rigorous. Both communicate that the compiler should be more exacting but both words have positive connotations (which invite usage rather than forbid it).

johnnyreilly avatar Aug 05 '14 06:08 johnnyreilly

--ultraprecise

I also like --safer (or simply --safe)

NoelAbrahams avatar Aug 05 '14 09:08 NoelAbrahams

--safe sounds good, I also like --warn:<n> flag of the C# compiler to adjust the warning level for the compiler.

knazeri avatar Aug 05 '14 10:08 knazeri

Perhaps the important question here is not so much the name but the compatibility promise associated with the flag. Will it only enforce a certain set of restrictions forevermore or can new restrictions be added under the same flag in future?

My personal preference would be to enable new restrictions by default under such a flag but provide opt-outs on a per-module basis that can be easily enabled to keep existing code compiling under new versions of tsc. The rationale being that it will improve the quality of the code in the TypeScript ecosystem as a whole.

I'd like to see --noImplicitAny made the default but that would break the important feature that new users can convert existing JS code to TypeScript just by changing the file extension.

robertknight avatar Aug 05 '14 10:08 robertknight

--warn:<n> seems like a nice idea. It's a bit more future proof, and deals with @robertknight's comment about adding new restrictions.

  • warn:0 - The default. Same as compiling without noimplicitany.
  • warn:1 - Adds --noimplicitany.
  • warn:2 - Disallow use before definition, etc. ...
  • warn:N - A future requirement

NoelAbrahams avatar Aug 05 '14 15:08 NoelAbrahams

C# compiler to adjust the warning level for the compiler.

@KamyarNazeri Is the error code from the compiler non-zero (e.g. 1) on warnings?

basarat avatar Aug 06 '14 05:08 basarat

The warning level for C# compiler is a zero-index value, 0 being the lowest (turns off emission of all warning messages) while higher numbers show more warnings: http://msdn.microsoft.com/en-us/library/13b90fz7.aspx

As mentioned by others, I too believe having levels of warnings could be extensible for future use

knazeri avatar Aug 06 '14 06:08 knazeri

Is the error code from the compiler non-zero (e.g. 1) on warnings?

The answer was on the page @KamyarNazeri mentioned for C#:

Use /warnaserror to treat all warnings as errors.

Since we want all of these to be errors perhaps we should use --error:<n> or we would need --warnAsError as well

basarat avatar Aug 06 '14 06:08 basarat