Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbsModel.ts#65062
Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbsModel.ts#65062jrieken merged 10 commits intomicrosoft:masterfrom
Conversation
| // | ||
| this.children = this._groups; | ||
| } else { | ||
| let group = first(this._groups); |
There was a problem hiding this comment.
Good catch -- I think I misread the intent of the code and though I could get away with checking if first(this._groups) is null, but that changes functionality. I'm appending a commit to this PR to reintroduce count
| readonly onDidChange = onDidChange.event; | ||
| getValue(overrides?: IConfigurationOverrides): T { | ||
| return service.getValue(name, overrides); | ||
| if (overrides) { |
There was a problem hiding this comment.
service.getValue(name, overrides) this should be good enough. Does the TS complains when using this?
There was a problem hiding this comment.
Yeah, I get
src/vs/workbench/browser/parts/editor/breadcrumbs.ts:93:37 - error TS2345: Argument of type 'IConfigurationOverrides | undefined' is not assignable to parameter of type 'IConfigurationOverrides'.
Type 'undefined' is not assignable to type 'IConfigurationOverrides'.
93 return service.getValue(name, overrides);
~~~~~~~~~
There was a problem hiding this comment.
I see.. looks like the API is strict here to not to accept undefined value for overrides. I can relax it if needed or you can go with above if/else branch.
@jrieken up to you.
| readonly onDidChange = onDidChange.event; | ||
| getValue(overrides?: IConfigurationOverrides): T { | ||
| return service.getValue(name, overrides); | ||
| if (overrides) { |
There was a problem hiding this comment.
service.getValue(name, overrides) this should be good enough. Does the TS complains when using this?
| constructor( | ||
| readonly id: string, | ||
| public parent: OutlineModel | OutlineGroup | OutlineElement, | ||
| public parent: TreeElement | undefined, |
There was a problem hiding this comment.
please refrain from making changes aren't needed for strict null checks
There was a problem hiding this comment.
I was surprised and spent quite a while trying to understand why, but this use of inheritance did cause errors under strict null checks. You can see them by checking out commit 9bbe60f (I separated the 'normal' null error fixes from the inheritance-related ones in separate commits).
src/vs/editor/contrib/documentSymbols/outlineModel.ts:90:2 - error TS2416: Property 'children' in type 'OutlineElement' is not assignable to the same property in base type 'TreeElement'.
Type '{ [id: string]: OutlineElement; }' is not assignable to type '{ [id: string]: TreeElement; }'.
Index signatures are incompatible.
Type 'OutlineElement' is not assignable to type 'TreeElement'.
Types of property 'parent' are incompatible.
Type 'OutlineElement | OutlineModel | OutlineGroup' is not assignable to type 'TreeElement'.
Type 'OutlineModel' is not assignable to type 'TreeElement'.
Types of property 'children' are incompatible.
Type '{ [id: string]: OutlineElement | OutlineGroup; }' is not assignable to type '{ [id: string]: TreeElement; }'.
Index signatures are incompatible.
Type 'OutlineElement | OutlineGroup' is not assignable to type 'TreeElement'.
Type 'OutlineGroup' is not assignable to type 'TreeElement'.
Types of property 'adopt' are incompatible.
Type '(parent: OutlineModel) => OutlineGroup' is not assignable to type '(newParent: TreeElement) => TreeElement'.
Types of parameters 'parent' and 'newParent' are incompatible.
Type 'TreeElement' is missing the following properties from type 'OutlineModel': _groups, textModel, _compact, merge, and 5 more.
90 children: { [id: string]: OutlineElement; } = Object.create(null);
I'm not 100% confident in my understand of this error but I think the error prevents a hypothetical error that could be make like this:
class SomeNewTypeOfTreeElement {
...
}
const tree: TreeElement = new OutlineElement(...)
tree.parent = new SomeNewTypeOfTreeElement(...) // this works because TreeElement doesn't know about OutlineElement
tree.doSomethingWithParentThatOutlineElementDoesNotExpect()
There's no such code right now, of course, but the type system doesn't care. Happy to learn if there's a better way to silence these errors though.
There was a problem hiding this comment.
Hm, intersting. Again, in doubt try to use the ! operator. My challenge is that it's hard to reason about these things but that I know that the code in its current shape runs. So, I'd prefer to not really change it
There was a problem hiding this comment.
I understand, but this just changes the type annotation from OutlineModel | OutlineGroup | OutlineElement to their parent TreeElement, so I think it's safe? See 38d4a54
| let marker = markers[start]; | ||
| myMarkers.push(marker); | ||
| markers[start] = undefined; | ||
| filteredMarkers[start] = undefined; |
There was a problem hiding this comment.
it's important to modify markers, don't create another another "name" for it
There was a problem hiding this comment.
The issue is that markers is of type Array<IMarker> which can't accept undefined values. By the end of the function, those undefined values are removed through coalesceInPlace, but in the meantime we need to let the type system know about the temporary undefineds.
I tried changing the type of markers to Array<IMarker | undefined> instead, but then more errors get throw for uses of binarySearch, Range.areIntersecting and others in that function so I went for a new alias as the solution that requires minimal code changes, though I agree it feels like a pretty hacky solution. Alternatives could include rewriting some of the logic to be a little it more functional (e.g. use filter) so that the array doesn't contain temporary undefined values, but that requires even more logic changes.
There was a problem hiding this comment.
when in doubt always use the ! operator. that makes it a lot easier for me/us to reason about these changes.
There was a problem hiding this comment.
Right that makes perfect sense, I'm just not sure how to apply it in this case because the ! can't operate in a templated type (turn Array<T | undefined> into Array<T>). In this case, markers is actually both Array<T | undefined> and Array<T> at different points in the function due to mutation.
What do you think of this other idea? Use type casting: (markers as Array<IMarker | undefined>)[start] = undefined;
| this.children = this._groups; | ||
| } else { | ||
| let group = first(this._groups); | ||
| if (group && count === 1) { |
There was a problem hiding this comment.
Please leave the code as it was before
There was a problem hiding this comment.
👍 going to use ! instead
| } | ||
|
|
||
| const buffer = this._editor.getModel(); | ||
| const editor = this._editor; |
|
@jrieken Any thoughts on the comments above? |
|
Sounds reasonable. I am in favour of adding casts et al because it make it simpler to reason about the changes for now. Please rebase and then I can merge |
|
@jrieken It's done -- apologies for the delay! |
|
Thanks. Merging! |
This PR enables strictNullChecks to
breadcrumbs.ts,outlineModel.tsandbreadcrumbsModel.tsas per #60565Most of it consisted of straightforward
| undefinedorif (...)additions. The one tricky bit is that subclasses ofTreeElementwere being clever in restricting their allowed parent classes, but that could technically be incorrect (e.g. if we later added code to reparentTreeElementsubtrees).