Skip to content

Commit b6d6b1f

Browse files
feat: audit "as any as" usage and prefer it over "as unknown as" (#144)
1 parent 1320e65 commit b6d6b1f

12 files changed

Lines changed: 45 additions & 43 deletions

File tree

packages/airtable-lib/src/connector/airtableRemoteConnector.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,31 @@ import { AirtableTableDao } from '../airtableTableDao.js'
1616

1717
export const AIRTABLE_CONNECTOR_REMOTE = Symbol('AIRTABLE_CONNECTOR_JSON')
1818

19-
export class AirtableRemoteConnector<BASE = any> implements AirtableConnector<BASE> {
19+
export class AirtableRemoteConnector<
20+
BASE extends Record<keyof BASE, AirtableRecord[]>,
21+
> implements AirtableConnector<BASE> {
2022
constructor(private airtableLib: AirtableLib) {}
2123

2224
readonly TYPE = AIRTABLE_CONNECTOR_REMOTE
2325

2426
async fetch(baseDaoCfg: AirtableBaseDaoCfg<BASE>, opt: AirtableDaoOptions = {}): Promise<BASE> {
2527
const { tableCfgMap } = baseDaoCfg
2628

27-
const base = {} as BASE
29+
const base: Record<string, AirtableRecord[]> = {}
2830

2931
await pMap(
3032
Object.keys(tableCfgMap),
3133
async tableName => {
32-
;(base as any)[tableName] = await this.getTableDao(
33-
baseDaoCfg,
34-
tableName as keyof BASE,
35-
).getRecords(opt)
34+
base[tableName] = await this.getTableDao(baseDaoCfg, tableName as keyof BASE).getRecords(
35+
opt,
36+
)
3637
},
3738
{
3839
concurrency: opt.concurrency || 4,
3940
},
4041
)
4142

42-
return base
43+
return base as BASE
4344
}
4445

4546
/**
@@ -71,7 +72,7 @@ export class AirtableRemoteConnector<BASE = any> implements AirtableConnector<BA
7172

7273
// One-by-one to preserve order
7374
await pMap(
74-
base[tableName] as any as AirtableRecord[],
75+
base[tableName],
7576
async _r => {
7677
const oldId = _r.airtableId
7778

@@ -112,7 +113,7 @@ export class AirtableRemoteConnector<BASE = any> implements AirtableConnector<BA
112113
async tableName => {
113114
const dao = this.getTableDao(baseDaoCfg, tableName)
114115

115-
const records = (base[tableName] as any as AirtableRecord[])
116+
const records = base[tableName]
116117
// Only records with non-empty array values
117118
.filter(r => Object.values(r).some(v => isArrayOfLinks(v)))
118119

@@ -124,7 +125,7 @@ export class AirtableRemoteConnector<BASE = any> implements AirtableConnector<BA
124125
let patch: AirtableRecord = _filterObject(r, (_k, v) => isArrayOfLinks(v))
125126
// console.log({patch1: patch})
126127
// use idMap
127-
patch = _mapValues(patch, (_k, v) => (v as any as string[]).map(oldId => idMap[oldId]))
128+
patch = _mapValues(patch, (_k, v) => (v as any).map((oldId: string) => idMap[oldId]))
128129
// console.log({patch2: patch})
129130
await dao.updateRecord(idMap[airtableId]!, patch, opt)
130131
},

packages/db-lib/src/timeseries/commonTimeSeriesDao.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class CommonTimeSeriesDao {
8383
if (q.fromIncl) dbq.filter('ts', '>=', q.fromIncl)
8484
if (q.toExcl) dbq.filter('ts', '<', q.toExcl)
8585

86-
const rows = (await this.cfg.db.runQuery(dbq)).rows as any as TimeSeriesRow[]
86+
const rows = (await this.cfg.db.runQuery(dbq)).rows as TimeSeriesRow[]
8787

8888
// todo: query from aggregated tables when step is above 'hour'
8989

File renamed without changes.

packages/dev-lib/cfg/oxlint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const sharedConfig = {
3939
// "vue",
4040
'vitest',
4141
],
42-
jsPlugins: [`${import.meta.dirname}/oxlint-plugin-stylistic.mjs`],
42+
jsPlugins: [`${import.meta.dirname}/oxlint-plugin-stylistic.js`],
4343
options: {
4444
reportUnusedDisableDirectives: 'deny',
4545
respectEslintDisableDirectives: false,

packages/dev-lib/src/test/cfg/oxlint.config.dump.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"plugins": ["unicorn", "typescript", "oxc", "import", "jsdoc", "vitest", "promise", "node"],
3-
"jsPlugins": ["cfg/oxlint-plugin-stylistic.mjs"],
3+
"jsPlugins": ["cfg/oxlint-plugin-stylistic.js"],
44
"categories": {},
55
"rules": {
66
"complexity": [

packages/dev-lib/src/vendor/mitm.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import type * as HttpType from 'node:http'
2+
import type * as HttpsType from 'node:https'
3+
import { createRequire } from 'node:module'
4+
import type * as NetType from 'node:net'
5+
import type * as TlsType from 'node:tls'
16
/**
27
* Minimal vendored implementation of the `mitm` package.
38
* Only supports the "connect" event with bypass() and disable() - that's all testOffline needs.
49
*
510
* Based on: https://github.com/moll/node-mitm
611
*/
7-
import type * as HttpType from 'node:http'
8-
import type * as HttpsType from 'node:https'
9-
import { createRequire } from 'node:module'
10-
import type * as NetType from 'node:net'
11-
import type * as TlsType from 'node:tls'
12+
import type { AnyObject } from '@naturalcycles/js-lib/types'
1213

1314
// Use require() to get mutable module objects (ESM namespace objects are frozen)
1415
const require = createRequire(import.meta.url)
@@ -116,7 +117,7 @@ export function createMitm(): Mitm {
116117
return function (...args: Parameters<typeof Tls.connect>): TlsType.TLSSocket {
117118
const [opts, done] = normalizeArgs(args)
118119
return connect(
119-
orig as unknown as (...a: unknown[]) => NetType.Socket,
120+
orig as (...a: unknown[]) => TlsType.TLSSocket,
120121
opts,
121122
done,
122123
) as TlsType.TLSSocket
@@ -129,12 +130,12 @@ export function createMitm(): Mitm {
129130

130131
stub(Net, 'connect', netConnect)
131132
stub(Net, 'createConnection', netConnect)
132-
stub(Http.Agent.prototype as unknown as Record<string, unknown>, 'createConnection', netConnect)
133+
stub(Http.Agent.prototype as AnyObject, 'createConnection', netConnect)
133134
stub(Tls, 'connect', tlsConnect)
134135

135136
// Disable keep-alive on global agents to force new connections
136-
const httpAgent = Http.globalAgent as unknown as Record<string, unknown>
137-
const httpsAgent = Https.globalAgent as unknown as Record<string, unknown>
137+
const httpAgent = Http.globalAgent as AnyObject
138+
const httpsAgent = Https.globalAgent as AnyObject
138139
if (httpAgent['keepAlive']) {
139140
stub(httpAgent, 'keepAlive', false)
140141
}

packages/js-lib/src/decorators/logMethod.decorator.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ test('methodReturnsArray', () => {
9797
})
9898

9999
test('instanceId', () => {
100-
const c = new C()
101-
;(c as any as InstanceId).instanceId = 'instance_1'
100+
const c = new C() as C & InstanceId
101+
c.instanceId = 'instance_1'
102102
c.syncMethodSuccess()
103103
})

packages/js-lib/src/object/keySortedMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class KeySortedMap<K, V> {
151151
forEach(cb: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
152152
const { map } = this
153153
for (const k of this.#sortedKeys) {
154-
cb.call(thisArg, map.get(k)!, k, this as unknown as Map<K, V>)
154+
cb.call(thisArg, map.get(k)!, k, this)
155155
}
156156
}
157157

packages/js-lib/src/object/lazyKeySortedMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class LazyKeySortedMap<K, V> {
128128
forEach(cb: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
129129
const { map } = this
130130
for (const k of this.getSortedKeys()) {
131-
cb.call(thisArg, map.get(k)!, k, this as unknown as Map<K, V>)
131+
cb.call(thisArg, map.get(k)!, k, this)
132132
}
133133
}
134134

packages/mongo-lib/src/mongo.db.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,16 @@ export class MongoDB extends BaseCommonDB implements CommonDB, AsyncDisposable {
153153
if (!ids.length) return []
154154

155155
const client = await this.client()
156-
const items = (await client
156+
const items = await client
157157
.db(this.cfg.db)
158-
.collection(table)
158+
.collection<MongoObject<ROW>>(table)
159159
.find({
160160
_id: {
161161
$in: ids as any[],
162162
},
163163
})
164-
.toArray()) as any as MongoObject<ROW>[]
165-
return items.map(i => this.mapFromMongo(i))
164+
.toArray()
165+
return items.map(i => this.mapFromMongo(i as MongoObject<ROW>))
166166
}
167167

168168
override async deleteByIds(
@@ -195,11 +195,11 @@ export class MongoDB extends BaseCommonDB implements CommonDB, AsyncDisposable {
195195
const client = await this.client()
196196
const { query, options } = dbQueryToMongoQuery(q)
197197

198-
const items = (await client
198+
const items = await client
199199
.db(this.cfg.db)
200200
.collection<ROW>(q.table)
201201
.find(query, options)
202-
.toArray()) as any as MongoObject<ROW>[]
202+
.toArray()
203203

204204
let rows = items.map(i => this.mapFromMongo(i as any))
205205

0 commit comments

Comments
 (0)