Skip to content

Commit 4a838dd

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Minor TypeScript improvements
GitOrigin-RevId: aa899f3c53b24ac0b85ba5eadc9ed01a3d617784
1 parent 2e0e6ae commit 4a838dd

26 files changed

+91
-145
lines changed

src/data/mrt/mrt.query.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ export function getPointXY([x, y]: [number, number], bandView: TBandViewRGBA, {s
4040
return null;
4141
}
4242

43-
let output: unknown[] = [];
43+
let output: ArrayLike<number> & {[index: number]: number} = [];
4444
if (scaled) {
4545
output = [];
4646
} else {
47-
const Ctor = bandView.data.constructor;
48-
// @ts-expect-error This expression is not constructable.
49-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
47+
const Ctor = bandView.data.constructor as new (size: number) => typeof bandView.data;
5048
output = new Ctor(dimension);
5149
}
5250

src/geo/projection/globe_util.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ export class GlobeSharedBuffers {
767767

768768
// Index adjustment, used to make strip (x, y) vertex input attribute data
769769
// to match same data on ordinary grid edges
770-
const prepareVertex = (x: number, y: number, isSkirt: boolean) => {
770+
const prepareVertex = (x: number, y: number, isSkirt: boolean): [number, number] => {
771771
if (!EMBED_SKIRTS) return [x, y];
772772

773773
let adjustedX = (() => {
@@ -790,7 +790,6 @@ export class GlobeSharedBuffers {
790790
// Add first horizontal strip if present
791791
if (EMBED_SKIRTS) {
792792
for (let x = 0; x < xVertices; ++x) {
793-
// @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
794793
vertices.emplaceBack(...prepareVertex(x, 0, true));
795794
}
796795
}
@@ -800,7 +799,6 @@ export class GlobeSharedBuffers {
800799
for (let x = 0; x < xVertices; ++x) {
801800
const isSideBorder = (x === 0 || x === xVertices - 1);
802801

803-
// @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
804802
vertices.emplaceBack(...prepareVertex(x, y, isSideBorder && EMBED_SKIRTS));
805803
}
806804
}
@@ -810,7 +808,6 @@ export class GlobeSharedBuffers {
810808
for (let lodIdx = 0; lodIdx < latitudinalLods.length; ++lodIdx) {
811809
const lastYRowForLod = latitudinalLods[lodIdx];
812810
for (let x = 0; x < xVertices; ++x) {
813-
// @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
814811
vertices.emplaceBack(...prepareVertex(x, lastYRowForLod, true));
815812
}
816813
}

src/render/draw_raster_particle.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,7 @@ export function prepare(layer: RasterParticleStyleLayer, sourceCache: SourceCach
555555
const band = layer.paint.get('raster-particle-array-band') || source.getInitialBand(sourceLayer);
556556
if (band == null) return;
557557

558-
// @ts-expect-error - TS2322 - Type 'Tile[]' is not assignable to type 'RasterArrayTile[]'.
559-
const tiles: Array<RasterArrayTile> = sourceCache.getIds().map(id => sourceCache.getTileByID(id));
558+
const tiles = sourceCache.getIds().map(id => sourceCache.getTileByID(id) as RasterArrayTile);
560559
for (const tile of tiles) {
561560
if (tile.updateNeeded(layer.id, band)) {
562561
source.prepareTile(tile, sourceLayer, layer.id, band);

src/render/glyph_manager.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Entry = {
5151
glyphs: StyleGlyphs;
5252
requests: {[range: number]: Array<Callback<GlyphRange>>};
5353
ranges: {[range: number]: boolean | null};
54-
tinySDF?: TinySDF;
54+
tinySDF?: TinySDF & {fontWeight: FontWeight};
5555
ascender?: number;
5656
descender?: number;
5757
};
@@ -225,7 +225,7 @@ class GlyphManager {
225225

226226
let tinySDF = entry.tinySDF;
227227
if (!tinySDF) {
228-
let fontWeight = '400';
228+
let fontWeight: FontWeight = '400';
229229
if (/bold/i.test(stack)) {
230230
fontWeight = '900';
231231
} else if (/medium/i.test(stack)) {
@@ -237,17 +237,15 @@ class GlyphManager {
237237
const fontSize = 24 * SDF_SCALE;
238238
const buffer = 3 * SDF_SCALE;
239239
const radius = 8 * SDF_SCALE;
240-
tinySDF = entry.tinySDF = new GlyphManager.TinySDF({fontFamily, fontWeight, fontSize, buffer, radius});
241-
// @ts-expect-error - TS2339 - Property 'fontWeight' does not exist on type 'TinySDF'.
240+
tinySDF = entry.tinySDF = new GlyphManager.TinySDF({fontFamily, fontWeight, fontSize, buffer, radius}) as Entry['tinySDF'];
242241
tinySDF.fontWeight = fontWeight;
243242
}
244243

245-
// @ts-expect-error - TS2339 - Property 'fontWeight' does not exist on type 'TinySDF'.
246-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
247-
if (this.localGlyphs[tinySDF.fontWeight][id]) {
248-
// @ts-expect-error - TS2339 - Property 'fontWeight' does not exist on type 'TinySDF'.
249-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
250-
return this.localGlyphs[tinySDF.fontWeight][id];
244+
const weight = tinySDF.fontWeight;
245+
246+
if (this.localGlyphs[weight][id]) {
247+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
248+
return this.localGlyphs[weight][id];
251249
}
252250

253251
const char = String.fromCodePoint(id);
@@ -271,9 +269,7 @@ class GlyphManager {
271269
*/
272270
const baselineAdjustment = 27;
273271

274-
// @ts-expect-error - TS2339 - Property 'fontWeight' does not exist on type 'TinySDF'.
275-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
276-
const glyph = this.localGlyphs[tinySDF.fontWeight][id] = {
272+
const glyph = this.localGlyphs[weight][id] = {
277273
id,
278274
bitmap: new AlphaImage({width, height}, data),
279275
metrics: {

src/source/custom_source.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ class CustomSource<T> extends Evented<SourceEvents> implements ISource {
212212
this.tileBounds = new TileBounds(this._implementation.bounds, this.minzoom, this.maxzoom);
213213
}
214214

215-
// @ts-expect-error - TS2339 - Property 'update' does not exist on type 'CustomSourceInterface<T>'.
216-
implementation.update = this._update.bind(this);
217-
218-
// @ts-expect-error - TS2339 - Property 'clearTiles' does not exist on type 'CustomSourceInterface<T>'.
219-
implementation.clearTiles = this._clearTiles.bind(this);
220-
221-
// @ts-expect-error - TS2339 - Property 'coveringTiles' does not exist on type 'CustomSourceInterface<T>'.
222-
implementation.coveringTiles = this._coveringTiles.bind(this);
215+
const impl = implementation as CustomSourceInterface<T> & {
216+
update: () => void;
217+
clearTiles: () => void;
218+
coveringTiles: () => {z: number; x: number; y: number}[];
219+
};
220+
impl.update = this._update.bind(this);
221+
impl.clearTiles = this._clearTiles.bind(this);
222+
impl.coveringTiles = this._coveringTiles.bind(this);
223223

224224
Object.assign(this, pick(implementation, ['dataType', 'scheme', 'minzoom', 'maxzoom', 'tileSize', 'attribution', 'minTileCacheSize', 'maxTileCacheSize']));
225225
}

src/source/geojson_wrapper.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ class FeatureWrapper implements VectorTileFeature {
4747
// vector tile spec only supports integer values for feature ids --
4848
// allowing non-integer values here results in a non-compliant PBF
4949
// that causes an exception when it is parsed with vector-tile-js
50-
// @ts-expect-error - TS2345 - Argument of type 'unknown' is not assignable to parameter of type 'number'.
51-
if ('id' in feature && !isNaN(feature.id)) {
52-
// @ts-expect-error - TS2345 - Argument of type 'unknown' is not assignable to parameter of type 'string'.
53-
this.id = parseInt(feature.id, 10);
50+
if ('id' in feature && !isNaN(feature.id as number)) {
51+
this.id = parseInt(feature.id as string, 10);
5452
}
5553
}
5654

src/source/rtl_text_plugin.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ let _completionCallback = null;
3030
let pluginStatus: PluginStatus = rtlPluginStatus.unavailable;
3131
let pluginURL: string | null | undefined = null;
3232

33-
export const triggerPluginCompletionEvent = function (error?: Error | null) {
33+
export const triggerPluginCompletionEvent = function (error?: Error | string | null) {
3434
// NetworkError's are not correctly reflected by the plugin status which prevents reloading plugin
35-
// @ts-expect-error - TS2339 - Property 'indexOf' does not exist on type 'never'.
36-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
3735
if (error && typeof error === 'string' && error.indexOf('NetworkError') > -1) {
3836
pluginStatus = rtlPluginStatus.error;
3937
}

src/source/source_cache.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,11 @@ class SourceCache extends Evented {
906906

907907
const expiryTimeout = tile.getExpiryTimeout();
908908
if (expiryTimeout) {
909-
// @ts-expect-error - TS2322 - Type 'Timeout' is not assignable to type 'number'.
909+
// Browser setTimeout returns number; cast needed because TS sees Node's Timeout type
910910
this._timers[id] = setTimeout(() => {
911911
this._reloadTile(id, 'expired');
912912
delete this._timers[id];
913-
}, expiryTimeout);
913+
}, expiryTimeout) as unknown as number;
914914
}
915915
}
916916

@@ -934,8 +934,7 @@ class SourceCache extends Evented {
934934
return;
935935

936936
if ((tile.hasData() && tile.state !== 'reloading') || tile.state === 'empty') {
937-
// @ts-expect-error - TS2345 - Argument of type 'number | void' is not assignable to parameter of type 'number'.
938-
this._cache.add(tile.tileID, tile, tile.getExpiryTimeout());
937+
this._cache.add(tile.tileID, tile, tile.getExpiryTimeout() as number | undefined);
939938
} else {
940939
tile.aborted = true;
941940
this._abortTile(tile);

src/source/tile.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export type ExpiryData = {
7474
// a tile bounds outline used for getting reprojected tile geometry in non-mercator projections
7575
const BOUNDS_FEATURE = (() => {
7676
return {
77-
type: 2,
77+
type: 2 as const,
7878
extent: EXTENT,
7979
loadGeometry() {
8080
return [[
@@ -903,7 +903,6 @@ class Tile {
903903
if (!projection || projection.name === 'mercator' || this._tileDebugBuffer) return;
904904

905905
// reproject tile outline with adaptive resampling
906-
// @ts-expect-error - TS2345 - Argument of type '{ type: number; extent: number; loadGeometry(): Point[][]; }' is not assignable to parameter of type 'FeatureWithGeometry'.
907906
const boundsLine = loadGeometry(BOUNDS_FEATURE, this.tileID.canonical, this.tileTransform)[0];
908907

909908
// generate vertices for debugging tile boundaries
@@ -926,7 +925,6 @@ class Tile {
926925
if (this._tileBoundsBuffer || !projection || projection.name === 'mercator') return;
927926

928927
// reproject tile outline with adaptive resampling
929-
// @ts-expect-error - TS2345 - Argument of type '{ type: number; extent: number; loadGeometry(): Point[][]; }' is not assignable to parameter of type 'FeatureWithGeometry'.
930928
const boundsLine = loadGeometry(BOUNDS_FEATURE, this.tileID.canonical, this.tileTransform)[0];
931929

932930
let boundsVertices, boundsIndices;

src/style-spec/expression/compound_expression.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class CompoundExpression implements Expression {
6767
const type = Array.isArray(definition) ?
6868
definition[0] : definition.type;
6969

70-
const availableOverloads = Array.isArray(definition) ?
70+
const availableOverloads: Array<[Signature, Evaluate]> = Array.isArray(definition) ?
7171
[[definition[1], definition[2]]] :
7272
definition.overloads;
7373

@@ -93,13 +93,10 @@ class CompoundExpression implements Expression {
9393
let argParseFailed = false;
9494
for (let i = 1; i < args.length; i++) {
9595
const arg = args[i];
96-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
9796
const expectedType = Array.isArray(params) ?
9897
params[i - 1] :
99-
// @ts-expect-error - TS2339 - Property 'type' does not exist on type 'Varargs | Evaluate'.
10098
params.type;
10199

102-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
103100
const parsed = signatureContext.parse(arg, 1 + parsedArgs.length, expectedType);
104101
if (!parsed) {
105102
argParseFailed = true;
@@ -121,16 +118,12 @@ class CompoundExpression implements Expression {
121118
}
122119

123120
for (let i = 0; i < parsedArgs.length; i++) {
124-
// @ts-expect-error - TS2339 - Property 'type' does not exist on type 'Varargs | Evaluate'.
125-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
126121
const expected = Array.isArray(params) ? params[i] : params.type;
127122
const arg = parsedArgs[i];
128-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
129123
signatureContext.concat(i + 1).checkSubtype(expected, arg.type);
130124
}
131125

132126
if (signatureContext.errors.length === 0) {
133-
// @ts-expect-error - TS2345 - Argument of type 'Signature | Evaluate' is not assignable to parameter of type 'Evaluate'.
134127
return new CompoundExpression(op, type, evaluate, parsedArgs, overloadIndex);
135128
}
136129
}

0 commit comments

Comments
 (0)