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

Our general plan is to provide simpler flags (probably just --noImplicitAny and --stricter or whatever we would call it) for tsc and let third-party build tools provide fancier stuff. Since everything will have an error code, it will be straightforward for tools like grunt-ts to provide filtering on the errors provided by the compiler.

RyanCavanaugh avatar Aug 06 '14 16:08 RyanCavanaugh

Not sure if this has been mentioned before, but how about an option to restrict multiple uses of interface declarations? I've run into a case where I forgot that I had already used an interface name somewhere and in this case the 2 interfaces get merged - which I understand is useful - but in some cases it would be useful to be able to say that you want this interface to not collide with any other one, so in case another exists already, throw a compile error and let you know that you need to pick another name? Maybe with something like:

unique interface IMyInterface {}

andrewvarga avatar Sep 04 '14 21:09 andrewvarga

This is a tracking issue for other issue, all of which are in the category of changes to the type system rather than additional features or syntax. Please post suggestions for new syntax or features elsewhere. Thanks!

RyanCavanaugh avatar Sep 04 '14 21:09 RyanCavanaugh

perhaps this suggestion https://github.com/Microsoft/TypeScript/issues/185 could be included in the list

fdecampredon avatar Sep 08 '14 15:09 fdecampredon

Again, this is only for additional "rules", not new features or new syntax.

RyanCavanaugh avatar Sep 08 '14 16:09 RyanCavanaugh

I came here after discovering hidden bugs in my code while, which wouldn't have slipped through if TypeScript had said list of stricter features, so I'm very supportive of this idea.

But I want to ask the TypeScript team to consider making this mode (and --noImplicitAny) be possible to specify on a file-by-file basis, so we can actually use it.

The majority of real-world projects reference third party definitions and libraries, which are typically not coded to take into account features like --noImplicitAny. Using this flag project-wide results in hundreds of errors piling up in our IDEs, and makes it very impractical to keep the flags enabled, even though we want to use them.

It should be possible to implement a simple syntax to enable this only for files where we need this (ex. our files, not third party files), much like JavaScript's "use strict" pragma.

stanvass avatar Oct 30 '14 19:10 stanvass

Note #1266 as a potential new rule for only allowing addition and similar operators to work on types that 'make sense.' This this case adding functions does give a somewhat reasonable result (allowing you to inspect the name, args, and body) but that may or may not be a net win for some, and there're other types that certainly give useless output ([Object object]).

danquirk avatar Nov 25 '14 22:11 danquirk

Note #1740 as a potential additional error level, although it may be too much of the 'fork the language' case and there're better solutions if we do real 'this' typing in the core language.

danquirk avatar Jan 20 '15 20:01 danquirk

I think it would be really nice if I could ask the compiler to warn me when I do this:

var data:Model = JSON.parse(json);

Instead of "silently" assigning the value of type any to data:Model, I would want the compiler to complain.

The compiler would stop complaining if I provided a type assertion. For example:

var data:Model = <Model>JSON.parse(json);

I like this because I am assuming responsibility for the type cast. If json doesn't represent data that conforms to the Model interface, that's my fault.

Thoughts?

samwgoldman avatar Jan 27 '15 23:01 samwgoldman

You can get the desired behavior by changing the return type of JSON.parse to {}. Disallowing all uses of any in its intended form seems far too strict.

RyanCavanaugh avatar Jan 28 '15 00:01 RyanCavanaugh

Since you're looking for motivating examples for disallowing function argument bivariance: we have code where we pass around constructor functions. These constructors accept an argument of an interface, but one of them incorrectly assumed it would get one specific concrete implementation of this interface instead. We expected this kind of thing to be caught but instead it was a runtime bug.

hesselink avatar Jun 26 '15 13:06 hesselink

It would be good to have ability to raise warning/error for all properties which don't have   AccessibilityModifier like private | public | protected in the stricter mode.

ghost avatar Sep 13 '15 02:09 ghost

@markbook2 you can do that easily today with a regular expression( see below). Edit: But use tslint.

Plus, this is a meta-bug, so you should probably file a new issue and reference this.

afrische avatar Sep 13 '15 10:09 afrische

@markbook2 @afrische TSLint has a rule for enforcing explicit visibility on class members / methods called member-access

adidahiya avatar Sep 14 '15 17:09 adidahiya

I think --strict should include --noImplicitAny.

:+1: --noImplicitAny should be default, but obviously for backcompat reasons it can't be. But it should definitely be part of any '--strict' mode.

milesrout avatar Sep 21 '15 02:09 milesrout

Here is another motivating examples for disallowing function argument bivariance:

In our typescript code base, we have an interface for classes having an equals method

export interface Eq {
    equals(x: any): boolean;
}

Because of function argument bivariance, classes can implement this interface "incorrectly", that is by narrowing the argument type any. For example:

class C implements Eq {
    constructor(private c: number) {}
    // I would like to get an error here, stating that the method signature in 
    // the Eq interface does not match the implementation given here. 
    // In OO-languages such as C# or Java, you get this error.
    equals(that: C) {
        return this.getNum() === that.getNum();
    }
    getNum() {
        return this.c;
    }
}

class D {
    constructor(private d: number) {}
    equals(that: D) {
        return this.d === that.d;
    }
}

We now write a function that searches in an array of Eq-objects for an element:

function findInArray(arr: Array<Eq>, x: Eq): number {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].equals(x)) {
            return i;
        }
    }
    return -1;
}

Now it's rather easy to trigger an unexpected runtime error:

const arr = [new C(1), new C(2)];
const i = findInArray(arr, new D(2));
console.log(i);

The program now aborts with TypeError: undefined is not a function

skogsbaer avatar Dec 20 '15 13:12 skogsbaer

We have moved to a more piecemeal approach to such checks, e.g. control flow checks, strict this, strict null, etc.. feels like this issue is obsolete. @RyanCavanaugh any objections to closing it?

mhegazy avatar Feb 20 '16 06:02 mhegazy

Another case where bivariant arguments really hurt is the React and Redux world, where mismatches in React component props are one of the most frequent source of errors.

Currently it's perfectly valid to have:

interface MyComponentProps {
  title: string;
}
export class MyComponent extends React.Component<MyComponentProps, void> {
  // code that needs this.props.title
}
const MyComponentAlias: React.ComponentClass<{}> = MyComponent;
// use of MyComponentAlias blows up, as it doesn't require title, but title is needed for MyComponent

The const in the above example is not a common use case, but I'm currently trying to make react-redux (especially the connect function) typings stricter, so props interface changes would highlight other changes that need to be made, but the bivariance only allows checking that the property field types are right, not that all properties are present where they need to be (and therefore also allows typos).

magnushiie avatar Mar 11 '16 12:03 magnushiie

I would like a flag under which TypeScript would reject any program that it could not prove to be free of type errors at runtime.

In addition to proving that a large class of bugs (type errors) are impossible, an AOT compiler (for servers) for "sound" typescript could erase the types at runtime for a perf gain.

DemiMarie avatar Jul 10 '16 05:07 DemiMarie

@DemiMarie what you're describing is not broadly possible given the constraints of our type system. As one example of many, we have no plausible way to prevent mutation after aliasing a structure with a less-specific type introducing an incorrect value into that structure, e.g. :

const a = [1, 2, 3];
const b: Array<string | number> = a;
b[0] = 'oops';
console.log(a[0] + 1); // 'oops1'

We've done a good job implementing almost everything in the OP so I'm going to close this due to a lack of usefulness of a meta-issue. Some good discussion happening at #9642 about how to get these stricter options on by default in places where it wouldn't be troublesome.

RyanCavanaugh avatar Jul 12 '16 21:07 RyanCavanaugh