Skip to content

Commit d4bb523

Browse files
authored
Fix a few more type assertions (microsoft#223138)
For microsoft#211878
1 parent d4d2ee3 commit d4bb523

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

src/vs/base/common/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ export function validateConstraint(arg: unknown, constraint: TypeConstraint | un
190190
}
191191
}
192192

193+
/**
194+
* Helper type assertion that safely upcasts a type to a supertype.
195+
*
196+
* This can be used to make sure the argument correctly conforms to the subtype while still being able to pass it
197+
* to contexts that expects the supertype.
198+
*/
199+
export function upcast<Base, Sub extends Base>(x: Sub): Base {
200+
return x;
201+
}
202+
193203
type AddFirstParameterToFunction<T, TargetFunctionsReturnType, FirstParameter> = T extends (...args: any[]) => TargetFunctionsReturnType ?
194204
// Function: add param to function
195205
(firstArg: FirstParameter, ...args: Parameters<T>) => ReturnType<T> :

src/vs/platform/utilityProcess/electron-main/utilityProcess.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { removeDangerousEnvVariables } from 'vs/base/common/processes';
1818
import { deepClone } from 'vs/base/common/objects';
1919
import { isWindows } from 'vs/base/common/platform';
2020
import { isUNCAccessRestrictionsDisabled, getUNCHostAllowlist } from 'vs/base/node/unc';
21+
import { upcast } from 'vs/base/common/types';
2122

2223
export interface IUtilityProcessConfiguration {
2324

@@ -249,18 +250,18 @@ export class UtilityProcess extends Disposable {
249250
this.log('creating new...', Severity.Info);
250251

251252
// Fork utility process
252-
this.process = utilityProcess.fork(modulePath, args, {
253+
this.process = utilityProcess.fork(modulePath, args, upcast<ForkOptions, ForkOptions & {
254+
forceAllocationsToV8Sandbox?: boolean;
255+
respondToAuthRequestsFromMainProcess?: boolean;
256+
}>({
253257
serviceName,
254258
env,
255259
execArgv,
256260
allowLoadingUnsignedLibraries,
257261
forceAllocationsToV8Sandbox,
258262
respondToAuthRequestsFromMainProcess,
259263
stdio
260-
} as ForkOptions & {
261-
forceAllocationsToV8Sandbox?: Boolean;
262-
respondToAuthRequestsFromMainProcess?: boolean;
263-
});
264+
}));
264265

265266
// Register to events
266267
this.registerListeners(this.process, this.configuration, serviceName);

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AsyncIterableObject } from 'vs/base/common/async';
77
import { CancellationTokenSource } from 'vs/base/common/cancellation';
88
import * as errors from 'vs/base/common/errors';
99
import { Emitter, Event } from 'vs/base/common/event';
10-
import { combinedDisposable, IDisposable } from 'vs/base/common/lifecycle';
10+
import { combinedDisposable } from 'vs/base/common/lifecycle';
1111
import { Schemas, matchesScheme } from 'vs/base/common/network';
1212
import Severity from 'vs/base/common/severity';
1313
import { URI } from 'vs/base/common/uri';
@@ -1206,17 +1206,17 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
12061206
},
12071207
registerFileSearchProviderNew: (scheme: string, provider: vscode.FileSearchProviderNew) => {
12081208
checkProposedApiEnabled(extension, 'fileSearchProviderNew');
1209-
return <IDisposable>{ dispose: () => { } };
1209+
return { dispose: () => { } };
12101210
},
12111211
registerTextSearchProviderNew: (scheme: string, provider: vscode.TextSearchProviderNew) => {
12121212
checkProposedApiEnabled(extension, 'textSearchProviderNew');
1213-
return <IDisposable>{ dispose: () => { } };
1213+
return { dispose: () => { } };
12141214
},
12151215
registerAITextSearchProviderNew: (scheme: string, provider: vscode.AITextSearchProviderNew) => {
12161216
// there are some dependencies on textSearchProvider, so we need to check for both
12171217
checkProposedApiEnabled(extension, 'aiTextSearchProviderNew');
12181218
checkProposedApiEnabled(extension, 'textSearchProviderNew');
1219-
return <IDisposable>{ dispose: () => { } };
1219+
return { dispose: () => { } };
12201220
},
12211221
registerRemoteAuthorityResolver: (authorityPrefix: string, resolver: vscode.RemoteAuthorityResolver) => {
12221222
checkProposedApiEnabled(extension, 'resolvers');

src/vs/workbench/api/common/extHostTypeConverters.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ export namespace SymbolTag {
787787

788788
export namespace WorkspaceSymbol {
789789
export function from(info: vscode.SymbolInformation): search.IWorkspaceSymbol {
790-
return <search.IWorkspaceSymbol>{
790+
return {
791791
name: info.name,
792792
kind: SymbolKind.from(info.kind),
793793
tags: info.tags && info.tags.map(SymbolTag.from),
@@ -966,7 +966,7 @@ export namespace Hover {
966966

967967
export namespace EvaluatableExpression {
968968
export function from(expression: vscode.EvaluatableExpression): languages.EvaluatableExpression {
969-
return <languages.EvaluatableExpression>{
969+
return {
970970
range: Range.from(expression.range),
971971
expression: expression.expression
972972
};
@@ -980,24 +980,24 @@ export namespace EvaluatableExpression {
980980
export namespace InlineValue {
981981
export function from(inlineValue: vscode.InlineValue): languages.InlineValue {
982982
if (inlineValue instanceof types.InlineValueText) {
983-
return <languages.InlineValueText>{
983+
return {
984984
type: 'text',
985985
range: Range.from(inlineValue.range),
986986
text: inlineValue.text
987-
};
987+
} satisfies languages.InlineValueText;
988988
} else if (inlineValue instanceof types.InlineValueVariableLookup) {
989-
return <languages.InlineValueVariableLookup>{
989+
return {
990990
type: 'variable',
991991
range: Range.from(inlineValue.range),
992992
variableName: inlineValue.variableName,
993993
caseSensitiveLookup: inlineValue.caseSensitiveLookup
994-
};
994+
} satisfies languages.InlineValueVariableLookup;
995995
} else if (inlineValue instanceof types.InlineValueEvaluatableExpression) {
996-
return <languages.InlineValueExpression>{
996+
return {
997997
type: 'expression',
998998
range: Range.from(inlineValue.range),
999999
expression: inlineValue.expression
1000-
};
1000+
} satisfies languages.InlineValueExpression;
10011001
} else {
10021002
throw new Error(`Unknown 'InlineValue' type`);
10031003
}
@@ -1006,28 +1006,28 @@ export namespace InlineValue {
10061006
export function to(inlineValue: languages.InlineValue): vscode.InlineValue {
10071007
switch (inlineValue.type) {
10081008
case 'text':
1009-
return <vscode.InlineValueText>{
1009+
return {
10101010
range: Range.to(inlineValue.range),
10111011
text: inlineValue.text
1012-
};
1012+
} satisfies vscode.InlineValueText;
10131013
case 'variable':
1014-
return <vscode.InlineValueVariableLookup>{
1014+
return {
10151015
range: Range.to(inlineValue.range),
10161016
variableName: inlineValue.variableName,
10171017
caseSensitiveLookup: inlineValue.caseSensitiveLookup
1018-
};
1018+
} satisfies vscode.InlineValueVariableLookup;
10191019
case 'expression':
1020-
return <vscode.InlineValueEvaluatableExpression>{
1020+
return {
10211021
range: Range.to(inlineValue.range),
10221022
expression: inlineValue.expression
1023-
};
1023+
} satisfies vscode.InlineValueEvaluatableExpression;
10241024
}
10251025
}
10261026
}
10271027

10281028
export namespace InlineValueContext {
10291029
export function from(inlineValueContext: vscode.InlineValueContext): extHostProtocol.IInlineValueContextDto {
1030-
return <extHostProtocol.IInlineValueContextDto>{
1030+
return {
10311031
frameId: inlineValueContext.frameId,
10321032
stoppedLocation: Range.from(inlineValueContext.stoppedLocation)
10331033
};

src/vs/workbench/test/browser/workbenchTestServices.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ import { IEnterWorkspaceResult, IRecent, IRecentlyOpened, IWorkspaceFolderCreati
123123
import { IWorkspaceTrustManagementService, IWorkspaceTrustRequestService } from 'vs/platform/workspace/common/workspaceTrust';
124124
import { IExtensionTerminalProfile, IShellLaunchConfig, ITerminalBackend, ITerminalLogService, ITerminalProfile, TerminalIcon, TerminalLocation, TerminalShellType } from 'vs/platform/terminal/common/terminal';
125125
import { ICreateTerminalOptions, IDeserializedTerminalEditorInput, ITerminalConfigurationService, ITerminalEditorService, ITerminalGroup, ITerminalGroupService, ITerminalInstance, ITerminalInstanceService, TerminalEditorLocation } from 'vs/workbench/contrib/terminal/browser/terminal';
126-
import { assertIsDefined } from 'vs/base/common/types';
126+
import { assertIsDefined, upcast } from 'vs/base/common/types';
127127
import { IRegisterContributedProfileArgs, IShellLaunchConfigResolveOptions, ITerminalProfileProvider, ITerminalProfileResolverService, ITerminalProfileService } from 'vs/workbench/contrib/terminal/common/terminal';
128128
import { EditorResolverService } from 'vs/workbench/services/editor/browser/editorResolverService';
129129
import { FILE_EDITOR_INPUT_ID } from 'vs/workbench/contrib/files/common/files';
@@ -211,12 +211,7 @@ export class TestTextFileEditor extends TextFileEditor {
211211
}
212212

213213
setSelection(selection: Selection | undefined, reason: EditorPaneSelectionChangeReason): void {
214-
if (selection) {
215-
const options: ITextEditorOptions = { selection };
216-
this._options = options;
217-
} else {
218-
this._options = undefined;
219-
}
214+
this._options = selection ? upcast<IEditorOptions, ITextEditorOptions>({ selection }) : undefined;
220215

221216
this._onDidChangeSelection.fire({ reason });
222217
}

0 commit comments

Comments
 (0)