Skip to content

Commit 35ba71b

Browse files
committed
add Revived-type for generic revive-function
1 parent 0a318b0 commit 35ba71b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/vs/base/common/marshalling.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { VSBuffer } from 'vs/base/common/buffer';
77
import { regExpFlags } from 'vs/base/common/strings';
8-
import { URI } from 'vs/base/common/uri';
8+
import { URI, UriComponents } from 'vs/base/common/uri';
99

1010
export function stringify(obj: any): string {
1111
return JSON.stringify(obj, replacer);
@@ -33,23 +33,31 @@ function replacer(key: string, value: any): any {
3333
return value;
3434
}
3535

36-
export function revive(obj: any, depth = 0): any {
36+
37+
type Deserialize<T> = T extends UriComponents ? URI
38+
: T extends object
39+
? Revived<T>
40+
: T;
41+
42+
export type Revived<T> = { [K in keyof T]: Deserialize<T[K]> };
43+
44+
export function revive<T = any>(obj: any, depth = 0): Revived<T> {
3745
if (!obj || depth > 200) {
3846
return obj;
3947
}
4048

4149
if (typeof obj === 'object') {
4250

4351
switch ((<MarshalledObject>obj).$mid) {
44-
case 1: return URI.revive(obj);
45-
case 2: return new RegExp(obj.source, obj.flags);
52+
case 1: return <any>URI.revive(obj);
53+
case 2: return <any>new RegExp(obj.source, obj.flags);
4654
}
4755

4856
if (
4957
obj instanceof VSBuffer
5058
|| obj instanceof Uint8Array
5159
) {
52-
return obj;
60+
return <any>obj;
5361
}
5462

5563
if (Array.isArray(obj)) {

0 commit comments

Comments
 (0)