-
-
Notifications
You must be signed in to change notification settings - Fork 768
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
Improve typescript typings with strict null check #2620
Conversation
@@ -24,7 +39,7 @@ function nativeType(property) { | |||
case 'string': | |||
return 'string'; | |||
case 'enum': | |||
return Object.keys(property.values).map(v => JSON.stringify(v)).join(' | '); | |||
return Object.keys(property.values!).map(v => JSON.stringify(v)).join(' | '); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the ! operator partially defeats the purpose of strictNullChecks, and should only be done as last resort. Here, I think it would make sense to use union types for Property
, so the different property types are declared with the appropriate fields:
type BooleanProperty = {
type: 'boolean';
name: string;
};
type EnumProperty = {
type: 'enum';
name: string;
values: string[];
...
};
type Property = BooleanProperty | EnumProperty | ... ;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to pitch in 😀 I'll be happy to review any PR sent against this branch.
The more I play around this the less value I see in pushing this forward honestly. |
I agree with your assessment. I think that strictNullCheck is one of the least useful of the strict checks, especially in terms of introducing it to an existing project. We may want to look into the other strict checks though. |
It is difficult to adopt in a mature project, with years of loose type definitions to account for. Littering the code with for (const feature of features) {
const featureKey = feature.key;
// Use of featureKey without null check flagged by compiler
if (seenFeatures[featureKey.bucketInstanceId] === undefined) {
seenFeatures[featureKey.bucketInstanceId] = {};
}
if (seenFeatures[featureKey.bucketInstanceId][featureKey.featureIndex]) {
continue;
} If feature.key is guaranteed to be non-null, the type shouldn't indicate that it's optional. If it is optional, this code should check. The issue here is that a common |
Don't get me wrong, I'm all for improving the typings, but I find that the errors are not accurate in some cases, especially when the code is a bit more complicated like the following: maplibre-gl-js/src/data/bucket/line_bucket.ts Line 300 in b45b5b4
Adding some additional types for specific cases also isn't a great solution, defining TypeA and then TypeAButWithoutB just to satisfy this check doesn't feel in some cases like the right solution. What I can think of as a middle ground is to enable this check, fix the places where types are missing, incorrect, or needs attention and improve the code just a bit as separate PRs and then decide what to do on the remaining places and whether or not to introduce this flag. Having said that, making sure the public API is strict can be a lot easier, as I did in #2623. |
I'm not planning to follow this through. |
This is an initial commit.
Some things to take into consideration:
!
in order to solve theundefined
part.?
import type
so that we only import the relevant needed type.Generally speaking, there shouldn't be any logic changes.
You can use
npm run typecheck
to see a summary of the remaining issues.I think PRs against this branch would be the right way to move forward in terms of review
Resolves #2588