Skip to content

Commit 6003e02

Browse files
committed
feat: update type definition
1 parent 60850e7 commit 6003e02

File tree

7 files changed

+48
-25
lines changed

7 files changed

+48
-25
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@swc/core": "^1.2.80",
3535
"@types/jest": "^27.0.1",
3636
"@types/rfdc": "^1.1.0",
37+
"@types/wechat-miniprogram": "^3.0.0",
3738
"@typescript-eslint/eslint-plugin": "^2.23.0",
3839
"@typescript-eslint/parser": "^2.23.0",
3940
"codecov": "^3.7.0",
@@ -57,11 +58,10 @@
5758
"miniprogram-exparser": "^2.11.2",
5859
"miniprogram-simulate": "^1.2.9",
5960
"prettier": "^2.0.1",
61+
"proxy-polyfill": "^0.3.2",
6062
"ts-jest": "^27.0.5",
6163
"ts-node": "^10.2.1",
62-
"typescript": "^4.0.3",
63-
"proxy-polyfill": "^0.3.2",
64-
"@types/wechat-miniprogram": "^3.0.0"
64+
"typescript": "^4.5.4"
6565
},
6666
"dependencies": {
6767
"fast-deep-equal": "^2.0.1",

src/behavior.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/* eslint-disable @typescript-eslint/ban-types */
21
import rfdc from 'rfdc'
32
import deepEqual from 'fast-deep-equal'
43
import * as dataPath from './data-path'
54
import * as dataTracer from './data-tracer'
5+
import type { IRelatedPathValue } from './data-tracer'
66

77
const deepClone = rfdc({ proto: true })
88

9+
910
interface BehaviorExtend {
1011
// original
1112
data: Record<string, any>;
@@ -19,8 +20,8 @@ interface ObserversItem {
1920
}
2021

2122
interface ComputedWatchInfo {
22-
computedUpdaters: Array<any>;
23-
computedRelatedPathValues: Record<string, any>;
23+
computedUpdaters: Array<(...args: unknown[]) => boolean>;
24+
computedRelatedPathValues: Record<string, Array<IRelatedPathValue>>;
2425
watchCurVal: Record<string, any>;
2526
_triggerFromComputedAttached: Record<string, boolean>;
2627
}

src/data-path.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const WHITE_SPACE_CHAR_REGEXP = /^\s/
22

3-
const throwParsingError = function (path, index) {
3+
interface IParserState {
4+
index: number;
5+
length: number;
6+
}
7+
8+
const throwParsingError = (path: string, index: number) => {
49
throw new Error(
510
'Parsing data path "' +
611
path +
@@ -12,7 +17,7 @@ const throwParsingError = function (path, index) {
1217
)
1318
}
1419

15-
const parseArrIndex = function (path, state) {
20+
const parseArrIndex = (path: string, state: IParserState) => {
1621
const startIndex = state.index
1722
while (state.index < state.length) {
1823
const ch = path[state.index]
@@ -28,7 +33,7 @@ const parseArrIndex = function (path, state) {
2833
return parseInt(path.slice(startIndex, state.index), 10)
2934
}
3035

31-
const parseIdent = function (path, state) {
36+
const parseIdent = (path: string, state: IParserState) => {
3237
const startIndex = state.index
3338
const ch = path[startIndex]
3439
if (/^[_a-zA-Z$]/.test(ch)) {
@@ -47,7 +52,7 @@ const parseIdent = function (path, state) {
4752
return path.slice(startIndex, state.index)
4853
}
4954

50-
const parseSinglePath = function (path, state) {
55+
const parseSinglePath = (path: string, state: IParserState) => {
5156
const paths = [parseIdent(path, state)]
5257
const options = {
5358
deepCmp: false,
@@ -56,7 +61,7 @@ const parseSinglePath = function (path, state) {
5661
const ch = path[state.index]
5762
if (ch === '[') {
5863
state.index++
59-
paths.push(parseArrIndex(path, state))
64+
paths.push(`${parseArrIndex(path, state)}`)
6065
const nextCh = path[state.index]
6166
if (nextCh !== ']') throwParsingError(path, state.index)
6267
state.index++
@@ -81,7 +86,7 @@ const parseSinglePath = function (path, state) {
8186
return { path: paths, options }
8287
}
8388

84-
const parseMultiPaths = function (path, state) {
89+
const parseMultiPaths = (path: string, state: IParserState) => {
8590
while (WHITE_SPACE_CHAR_REGEXP.test(path[state.index])) {
8691
state.index++
8792
}
@@ -104,11 +109,11 @@ const parseMultiPaths = function (path, state) {
104109
return ret
105110
}
106111

107-
const parseEOF = function (path, state) {
112+
const parseEOF = (path: string, state: IParserState) => {
108113
if (state.index < state.length) throwParsingError(path, state.index)
109114
}
110115

111-
export function parseMultiDataPaths(path: string) {
116+
export const parseMultiDataPaths = (path: string) => {
112117
const state = {
113118
length: path.length,
114119
index: 0,
@@ -118,7 +123,7 @@ export function parseMultiDataPaths(path: string) {
118123
return ret
119124
}
120125

121-
export const getDataOnPath = function (data, path) {
126+
export const getDataOnPath = (data: unknown, path: Array<string>) => {
122127
let ret = data
123128
path.forEach((s) => {
124129
if (typeof ret !== 'object' || ret === null) ret = undefined

src/data-tracer.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import ProxyPolyfillBuilder from 'proxy-polyfill/src/proxy'
22
const ProxyPolyfill = ProxyPolyfillBuilder()
33

4-
const wrapData = (data, relatedPathValues, basePath) => {
4+
interface IWrappedData {
5+
__rawObject__: unknown;
6+
}
7+
8+
export interface IRelatedPathValue {
9+
path: Array<string>;
10+
value: unknown;
11+
}
12+
13+
const wrapData = (data: unknown, relatedPathValues: Array<IRelatedPathValue>, basePath: Array<string>) => {
514
if (typeof data !== 'object' || data === null) return data
615
const handler = {
7-
get(obj, key) {
16+
get(obj: unknown, key: string) {
817
if (key === '__rawObject__') return obj
918
let keyWrapper = null
1019
const keyPath = basePath.concat(key)
@@ -26,11 +35,11 @@ const wrapData = (data, relatedPathValues, basePath) => {
2635
return propDef
2736
}
2837

29-
export function create(data, relatedPathValues) {
38+
export function create(data: unknown, relatedPathValues: Array<IRelatedPathValue>) {
3039
return wrapData(data, relatedPathValues, [])
3140
}
3241

33-
export function unwrap(wrapped) {
42+
export function unwrap(wrapped: IWrappedData) {
3443
// #70
3544
if (typeof wrapped.__rawObject__ !== 'object' && typeof wrapped === 'object' && wrapped !== null) {
3645
if (Array.isArray(wrapped)) {

types/data-path.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export declare function parseMultiDataPaths(path: string): {
2-
path: any[];
1+
export declare const parseMultiDataPaths: (path: string) => {
2+
path: string[];
33
options: {
44
deepCmp: boolean;
55
};
66
}[];
7-
export declare const getDataOnPath: (data: any, path: any) => any;
7+
export declare const getDataOnPath: (data: unknown, path: Array<string>) => unknown;

types/data-tracer.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
export declare function create(data: any, relatedPathValues: any): any;
2-
export declare function unwrap(wrapped: any): any;
1+
interface IWrappedData {
2+
__rawObject__: unknown;
3+
}
4+
export interface IRelatedPathValue {
5+
path: Array<string>;
6+
value: unknown;
7+
}
8+
export declare function create(data: unknown, relatedPathValues: Array<IRelatedPathValue>): any;
9+
export declare function unwrap(wrapped: IWrappedData): any;
10+
export {};

0 commit comments

Comments
 (0)