Skip to content

Commit 89264d8

Browse files
committed
fix: object type checking
1 parent ad2109f commit 89264d8

File tree

8 files changed

+10
-43
lines changed

8 files changed

+10
-43
lines changed

src/errorReporting/typeOf.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export default function typeOf(value: any): string {
22
if (value == null) return String(value)
33
if (typeof value !== 'object') return typeof value
4-
if (value.constructor) return value.constructor.name
4+
if (value.constructor && value.constructor !== Object)
5+
return value.constructor.name
56
return `{\n${Object.keys(value)
6-
.map(key => ` ${key}: ${typeOf(value)}`)
7+
.map(key => ` ${key}: ${typeOf(value[key])}`)
78
.join(',\n')}\n}`
89
}

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const symbol = () => new SymbolType()
6161
export const symbolLiteral = (value: symbol) => new SymbolLiteralType(value)
6262

6363
export function object<S extends {}>(
64-
properties: { [K in keyof S]: Type<S[K]> }
64+
properties: { [K in keyof S]-?: Type<S[K]> }
6565
): Type<S> {
6666
return new ObjectType(
6767
Object.keys(properties).map(
@@ -87,12 +87,12 @@ type Tag = {
8787
PropagateAtLaunch?: boolean | null | undefined
8888
}
8989

90-
const TagType = object({
90+
const TagType = object<Tag>({
9191
Key: string(),
9292
Value: string(),
9393
PropagateAtLaunch: nullable(boolean()),
9494
})
9595

9696
const TagsType = new ArrayType(TagType)
9797

98-
TagsType.assert([{ Key: 'foo', Value: 'bar', Qux: 2, PropagateAtLaunch: true }])
98+
TagsType.assert([{ Key: 'foo', Value: 'bar', Qux: 2 }])

src/types/IntersectionType.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,6 @@ export default class IntersectionType<T extends {}> extends Type<T> {
102102
}
103103
}
104104

105-
unwrap(): ObjectType<T> {
106-
const properties: any = []
107-
const indexers = []
108-
const { types } = this
109-
for (let i = 0; i < types.length; i++) {
110-
const type = types[i].unwrap()
111-
invariant(type instanceof ObjectType, 'Can only intersect object types')
112-
indexers.push(...(type as ObjectType<any>).indexers)
113-
mergeProperties(properties, (type as ObjectType<any>).properties)
114-
}
115-
return new ObjectType<T>(properties, indexers, true)
116-
}
117-
118105
toString(): string {
119106
return this.types.join(' & ')
120107
}

src/types/NullableType.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import NullLiteralType from './NullLiteralType'
44
import VoidType from './VoidType'
55
import Validation, { ErrorTuple, IdentifierPath } from '../Validation'
66

7-
export default class NullableType<T> extends Type<T> {
7+
export default class NullableType<T> extends Type<T | null | undefined> {
88
typeName: string = 'NullableType'
99
type: Type<T>
1010

@@ -46,13 +46,6 @@ export default class NullableType<T> extends Type<T> {
4646
}
4747
}
4848

49-
/**
50-
* Get the inner type or value.
51-
*/
52-
unwrap(): Type<T> {
53-
return this
54-
}
55-
5649
toString(): string {
5750
return `${this.type.toString()} | null | undefined`
5851
}

src/types/ObjectType.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export default class ObjectType<T extends {}> extends Type<T> {
5151
return property
5252
}
5353
}
54-
return this.getIndexer(key)
5554
}
5655

5756
setProperty(
@@ -85,7 +84,7 @@ export default class ObjectType<T extends {}> extends Type<T> {
8584
return true
8685
}
8786
}
88-
return this.hasIndexer(key)
87+
return false
8988
}
9089

9190
/**

src/types/ObjectTypeIndexer.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ export default class ObjectTypeIndexer<
8787
}
8888
}
8989

90-
unwrap(): Type<V> {
91-
return this.value.unwrap()
92-
}
93-
9490
toString(): string {
9591
return `[${this.id}: ${this.key.toString()}]: ${this.value.toString()};`
9692
}

src/types/ObjectTypeProperty.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ export default class ObjectTypeProperty<
144144
}
145145
}
146146

147-
unwrap(): Type<V> {
148-
return this.value.unwrap()
149-
}
150-
151147
toString(): string {
152148
let key: any = this.key
153149
if (typeof key === 'symbol') {

src/types/Type.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { ErrorTuple, IdentifierPath } from '../Validation'
99
* This is the base class for all types.
1010
*/
1111
export default class Type<T> {
12+
readonly __type: T = null as any
13+
readonly __constraint: (value: T) => any = null as any
1214
typeName: string = 'Type'
1315

1416
constructor() {}
@@ -51,13 +53,6 @@ export default class Type<T> {
5153
return input
5254
}
5355

54-
/**
55-
* Get the inner type.
56-
*/
57-
unwrap(): Type<T> {
58-
return this
59-
}
60-
6156
toString() {
6257
return '$Type'
6358
}

0 commit comments

Comments
 (0)