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

Implement strict mode #182

Open
andreas-aeschlimann opened this issue May 12, 2020 · 4 comments
Open

Implement strict mode #182

andreas-aeschlimann opened this issue May 12, 2020 · 4 comments
Assignees
Labels
enhancement Improve existing code or new feature

Comments

@andreas-aeschlimann
Copy link
Contributor

Today, I have learned about some new (~1-2 year old) compiler options of TypeScript which help to write better code and prevent errors. That is why I suggest that we implement this flag (called strict mode) in this library and also in all Angular/TypeScript projects that we have.

From the documentation:

Enable all strict type checking options.
Enabling --strict enables --noImplicitAny, --noImplicitThis, --alwaysStrict, --strictBindCallApply, --strictNullChecks, --strictFunctionTypes and --strictPropertyInitialization.

Source: https://www.typescriptlang.org/docs/handbook/compiler-options.html

To summarize: The compiler option strict enables seven flags that help you write better code. It is possible that in future releases more flags will be added.

As of now, we only have two flags set in this library: noImplicitAny and strictNullChecks. I suggest we remove those two flags from tsconfig.json and replace it with strict: true.

@tobiasschweizer
Copy link
Contributor

Thanks for this, I will move it to youtrack.

Btw: I ordered new book about TypeScript that should cover this too :-)

@andreas-aeschlimann
Copy link
Contributor Author

andreas-aeschlimann commented May 12, 2020

The strict flag forbids you to do things like that:

export class User {
    name: string;
}

Then:

const user = new User();
console.log(user.name); // undefined even though we told the compiler it should be string

I have dicussed implications of this issue with @tobiasschweizer on the phone lately. Basically, as of now, you can define such a User class which never assigns a name. It basically means that name can be undefined and it should be flagged as such.

With the strict flags, developers are forced to write name: string | undefined or assign a value in the constructor.

@tobiasschweizer
Copy link
Contributor

I think the flag you are talking about is strictPropertyInitialization .

@andreas-aeschlimann
Copy link
Contributor Author

Yes, but strict is a superset of that and thus better. Microsoft/Typescript states that in future they may add more flags that will be still covered if strict is set to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve existing code or new feature
Projects
None yet
Development

No branches or pull requests

2 participants