Skip to content

Commit

Permalink
Use As Never Convention
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 26, 2024
1 parent 8a882b9 commit 339491e
Show file tree
Hide file tree
Showing 70 changed files with 165 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/type/any/any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ export interface TAny extends TSchema {

/** `[Json]` Creates an Any type */
export function Any(options: SchemaOptions = {}): TAny {
return { ...options, [Kind]: 'Any' } as unknown as TAny
return { ...options, [Kind]: 'Any' } as never
}
2 changes: 1 addition & 1 deletion src/type/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ export function Array<T extends TSchema>(schema: T, options: ArrayOptions = {}):
[Kind]: 'Array',
type: 'array',
items: CloneType(schema),
} as unknown as TArray<T>
} as never
}
2 changes: 1 addition & 1 deletion src/type/async-iterator/async-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export function AsyncIterator<T extends TSchema>(items: T, options: SchemaOption
[Kind]: 'AsyncIterator',
type: 'AsyncIterator',
items: CloneType(items),
} as unknown as TAsyncIterator<T>
} as never
}
10 changes: 5 additions & 5 deletions src/type/awaited/awaited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> =
: Acc
// prettier-ignore
function FromRest<T extends TSchema[]>(T: [...T]) : TFromRest<T> {
return T.map(L => AwaitedResolve(L)) as TFromRest<T>
return T.map(L => AwaitedResolve(L)) as never
}
// ----------------------------------------------------------------
// FromIntersect
Expand All @@ -55,7 +55,7 @@ function FromRest<T extends TSchema[]>(T: [...T]) : TFromRest<T> {
type TFromIntersect<T extends TSchema[]> = TIntersect<TFromRest<T>>
// prettier-ignore
function FromIntersect<T extends TSchema[]>(T: [...T]): TFromIntersect<T> {
return Intersect(FromRest(T) as TSchema[]) as unknown as TFromIntersect<T>
return Intersect(FromRest(T) as TSchema[]) as never
}
// ----------------------------------------------------------------
// FromUnion
Expand All @@ -64,15 +64,15 @@ function FromIntersect<T extends TSchema[]>(T: [...T]): TFromIntersect<T> {
type TFromUnion<T extends TSchema[]> = TUnion<TFromRest<T>>
// prettier-ignore
function FromUnion<T extends TSchema[]>(T: [...T]): TFromUnion<T> {
return Union(FromRest(T) as TSchema[]) as unknown as TFromUnion<T>
return Union(FromRest(T) as TSchema[]) as never
}
// ----------------------------------------------------------------
// Promise
// ----------------------------------------------------------------
type TFromPromise<T extends TSchema> = TAwaited<T>
// prettier-ignore
function FromPromise<T extends TSchema>(T: T): TFromPromise<T> {
return AwaitedResolve(T) as TFromPromise<T>
return AwaitedResolve(T) as never
}
// ----------------------------------------------------------------
// AwaitedResolve
Expand All @@ -84,7 +84,7 @@ function AwaitedResolve<T extends TSchema>(T: T): TAwaited<T> {
IsUnion(T) ? FromUnion(T.anyOf) :
IsPromise(T) ? FromPromise(T.item) :
T
) as TAwaited<T>
) as never
}
// ------------------------------------------------------------------
// TAwaited
Expand Down
2 changes: 1 addition & 1 deletion src/type/bigint/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export function BigInt(options: BigIntOptions = {}): TBigInt {
...options,
[Kind]: 'BigInt',
type: 'bigint',
} as TBigInt
} as never
}
2 changes: 1 addition & 1 deletion src/type/boolean/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export function Boolean(options: SchemaOptions = {}): TBoolean {
...options,
[Kind]: 'Boolean',
type: 'boolean',
} as unknown as TBoolean
} as never
}
2 changes: 1 addition & 1 deletion src/type/clone/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { Clone } from './value'

/** Clones a Rest */
export function CloneRest<T extends TSchema[]>(schemas: T): T {
return schemas.map((schema) => CloneType(schema)) as T
return schemas.map((schema) => CloneType(schema)) as never
}
/** Clones a Type */
export function CloneType<T extends TSchema>(schema: T, options: SchemaOptions = {}): T {
Expand Down
2 changes: 1 addition & 1 deletion src/type/composite/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ export function Composite<T extends TSchema[]>(T: [...T], options: ObjectOptions
const K = CompositeKeys(T)
const P = CompositeProperties(T, K)
const R = Object(P, options)
return R as TComposite<T>
return R as never
}
8 changes: 4 additions & 4 deletions src/type/const/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type TFromArray<T extends readonly unknown[]> =
: T
// prettier-ignore
function FromArray<T extends readonly unknown[]>(T: [...T]): TFromArray<T> {
return T.map(L => FromValue(L, false)) as TFromArray<T>
return T.map(L => FromValue(L, false)) as never
}
// ------------------------------------------------------------------
// FromProperties
Expand All @@ -82,7 +82,7 @@ function FromProperties<T extends Record<PropertyKey, unknown>>(value: T): TFrom
// ------------------------------------------------------------------
type TConditionalReadonly<T extends TSchema, Root extends boolean> = Root extends true ? T : TReadonly<T>
function ConditionalReadonly<T extends TSchema, Root extends boolean>(T: T, root: Root): TConditionalReadonly<T, Root> {
return (root === true ? T : Readonly(T)) as unknown as TConditionalReadonly<T, Root>
return (root === true ? T : Readonly(T)) as never
}
// ------------------------------------------------------------------
// FromValue
Expand Down Expand Up @@ -122,7 +122,7 @@ function FromValue<T, Root extends boolean>(value: T, root: Root): FromValue<T,
IsBoolean(value) ? Literal(value) :
IsString(value) ? Literal(value) :
Object({})
) as FromValue<T, Root>
) as never
}
// ------------------------------------------------------------------
// TConst
Expand All @@ -131,5 +131,5 @@ export type TConst<T> = FromValue<T, true>

/** `[JavaScript]` Creates a readonly const type from the given value. */
export function Const</* const (not supported in 4.0) */ T>(T: T, options: SchemaOptions = {}): TConst<T> {
return CloneType(FromValue(T, true), options) as TConst<T>
return CloneType(FromValue(T, true), options) as never
}
2 changes: 1 addition & 1 deletion src/type/constructor/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export function Constructor<T extends TSchema[], U extends TSchema>(parameters:
type: 'Constructor',
parameters: CloneRest(parameters),
returns: CloneType(returns),
} as unknown as TConstructor<T, U>
} as never
}
2 changes: 1 addition & 1 deletion src/type/date/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ export function Date(options: DateOptions = {}): TDate {
...options,
[Kind]: 'Date',
type: 'Date',
} as unknown as TDate
} as never
}
2 changes: 1 addition & 1 deletion src/type/deref/deref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function DerefResolve<T extends TSchema>(schema: T, references: TSchema[]): TDer
IsIterator(schema) ? FromIterator(schema, references) :
IsRef(schema) ? FromRef(schema, references) :
schema
) as TDeref<T>
) as never
}
// prettier-ignore
export type TDeref<T extends TSchema> =
Expand Down
2 changes: 1 addition & 1 deletion src/type/enum/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ export function Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item:
.map((key) => item[key]) as T[keyof T][]
const values2 = [...new Set(values1)]
const anyOf = values2.map((value) => Literal(value))
return Union(anyOf, { ...options, [Hint]: 'Enum' }) as unknown as TEnum<T>
return Union(anyOf, { ...options, [Hint]: 'Enum' }) as never
}
6 changes: 3 additions & 3 deletions src/type/exclude/exclude-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function FromMappedResult<
R extends TMappedResult,
T extends TSchema
>(R: R, T: T): TFromMappedResult<R, T> {
return FromProperties(R.properties, T) as TFromMappedResult<R, T>
return FromProperties(R.properties, T) as never
}
// ------------------------------------------------------------------
// ExcludeFromMappedResult
Expand All @@ -84,6 +84,6 @@ export function ExcludeFromMappedResult<
T extends TSchema,
P extends TProperties = TFromMappedResult<R, T>
>(R: R, T: T): TMappedResult<P> {
const P = FromMappedResult(R, T) as unknown as P
return MappedResult(P)
const P = FromMappedResult(R, T)
return MappedResult(P) as never
}
10 changes: 5 additions & 5 deletions src/type/extends/extends-from-mapped-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function FromPropertyKey<
>(K: K, U: U, L: L, R: R, options: SchemaOptions): TFromPropertyKey<K, U, L, R> {
return {
[K]: Extends(Literal(K as TLiteralValue), U, L, R, options) as any
} as TFromPropertyKey<K, U, L, R>
} as never
}
// ------------------------------------------------------------------
// FromPropertyKeys
Expand All @@ -80,7 +80,7 @@ function FromPropertyKeys<
>(K: [...K], U: U, L: L, R: R, options: SchemaOptions): TFromPropertyKeys<K, U, L, R> {
return K.reduce((Acc, LK) => {
return { ...Acc, ...FromPropertyKey(LK, U, L, R, options) }
}, {} as TProperties) as TFromPropertyKeys<K, U, L, R>
}, {} as TProperties) as never
}
// ------------------------------------------------------------------
// FromMappedKey
Expand All @@ -101,7 +101,7 @@ function FromMappedKey<
L extends TSchema,
R extends TSchema
>(K: K, U: U, L: L, R: R, options: SchemaOptions): TFromMappedKey<K, U, L, R> {
return FromPropertyKeys(K.keys, U, L, R, options) as TFromMappedKey<K, U, L, R>
return FromPropertyKeys(K.keys, U, L, R, options) as never
}
// ------------------------------------------------------------------
// ExtendsFromMappedKey
Expand All @@ -124,6 +124,6 @@ export function ExtendsFromMappedKey<
R extends TSchema,
P extends TProperties = TFromMappedKey<T, U, L, R>
>(T: T, U: U, L: L, R: R, options: SchemaOptions): TMappedResult<P> {
const P = FromMappedKey(T, U, L, R, options) as unknown as P
return MappedResult(P)
const P = FromMappedKey(T, U, L, R, options)
return MappedResult(P) as never
}
6 changes: 3 additions & 3 deletions src/type/extends/extends-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function FromMappedResult<
True extends TSchema,
False extends TSchema
>(Left: Left, Right: Right, True: True, False: False, options: SchemaOptions): TFromMappedResult<Left, Right, True, False> {
return FromProperties(Left.properties, Right, True, False, options) as TFromMappedResult<Left, Right, True, False>
return FromProperties(Left.properties, Right, True, False, options) as never
}
// ------------------------------------------------------------------
// ExtendsFromMappedResult
Expand All @@ -96,6 +96,6 @@ export function ExtendsFromMappedResult<
False extends TSchema,
P extends TProperties = TFromMappedResult<Left, Right, True, False>
>(Left: Left, Right: Right, True: True, False: False, options: SchemaOptions): TMappedResult<P> {
const P = FromMappedResult(Left, Right, True, False, options) as unknown as P
return MappedResult(P)
const P = FromMappedResult(Left, Right, True, False, options)
return MappedResult(P) as never
}
4 changes: 2 additions & 2 deletions src/type/extends/extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function ExtendsResolve<L extends TSchema, R extends TSchema, T extends TSchema,
R === ExtendsResult.Union ? Union([trueType, falseType]) :
R === ExtendsResult.True ? trueType :
falseType
) as unknown as TExtendsResolve<L, R, T, U>
) as never
}
// ------------------------------------------------------------------
// TExtends
Expand All @@ -76,5 +76,5 @@ export function Extends<L extends TSchema, R extends TSchema, T extends TSchema,
IsMappedResult(L) ? ExtendsFromMappedResult(L, R, T, F, options) :
IsMappedKey(L) ? CloneType(ExtendsFromMappedKey(L, R, T, F, options)) :
CloneType(ExtendsResolve(L, R, T, F), options)
) as TExtends<L, R, T, F>
) as never
}
6 changes: 3 additions & 3 deletions src/type/extract/extract-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function FromMappedResult<
R extends TMappedResult,
T extends TSchema
>(R: R, T: T): TFromMappedResult<R, T> {
return FromProperties(R.properties, T) as TFromMappedResult<R, T>
return FromProperties(R.properties, T) as never
}
// ------------------------------------------------------------------
// ExtractFromMappedResult
Expand All @@ -84,6 +84,6 @@ export function ExtractFromMappedResult<
T extends TSchema,
P extends TProperties = TFromMappedResult<R, T>
>(R: R, T: T): TMappedResult<P> {
const P = FromMappedResult(R, T) as unknown as P
return MappedResult(P)
const P = FromMappedResult(R, T)
return MappedResult(P) as never
}
2 changes: 1 addition & 1 deletion src/type/function/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ export function Function<T extends TSchema[], U extends TSchema>(parameters: [..
type: 'Function',
parameters: CloneRest(parameters),
returns: CloneType(returns),
} as unknown as TFunction<T, U>
} as never
}
2 changes: 1 addition & 1 deletion src/type/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type IncrementReverse<T extends string> = T extends `${infer L}${infer R}` ? `${
export type TIncrement<T extends string> = IncrementReverse<IncrementStep<IncrementReverse<T>>>
/** Increments the given string value + 1 */
export function Increment<T extends string>(T: T): TIncrement<T> {
return (parseInt(T) + 1).toString() as TIncrement<T>
return (parseInt(T) + 1).toString() as never
}
// ------------------------------------------------------------------
// Helper: Type Asserts
Expand Down
10 changes: 5 additions & 5 deletions src/type/indexed/indexed-from-mapped-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function MappedIndexPropertyKey<
T extends TSchema,
K extends PropertyKey
>(T: T, K: K, options: SchemaOptions): TMappedIndexPropertyKey<T, K> {
return { [K]: Index(T, [K], options) } as TMappedIndexPropertyKey<T, K>
return { [K]: Index(T, [K], options) } as never
}
// ------------------------------------------------------------------
// MappedIndexPropertyKeys
Expand All @@ -65,7 +65,7 @@ function MappedIndexPropertyKeys<
>(T: T, K: [...K], options: SchemaOptions): TMappedIndexPropertyKeys<T, K> {
return K.reduce((Acc, L) => {
return { ...Acc, ...MappedIndexPropertyKey(T, L, options) }
}, {} as TProperties) as TMappedIndexPropertyKeys<T, K>
}, {} as TProperties) as never
}
// ------------------------------------------------------------------
// MappedIndexProperties
Expand All @@ -79,7 +79,7 @@ function MappedIndexProperties<
T extends TSchema,
K extends TMappedKey
>(T: T, K: K, options: SchemaOptions): TMappedIndexProperties<T, K> {
return MappedIndexPropertyKeys(T, K.keys, options) as TMappedIndexProperties<T, K>
return MappedIndexPropertyKeys(T, K.keys, options) as never
}
// ------------------------------------------------------------------
// TIndexFromMappedKey
Expand All @@ -98,6 +98,6 @@ export function IndexFromMappedKey<
K extends TMappedKey,
P extends TProperties = TMappedIndexProperties<T, K>
>(T: T, K: K, options: SchemaOptions): TMappedResult<P> {
const P = MappedIndexProperties(T, K, options) as unknown as P
return MappedResult(P)
const P = MappedIndexProperties(T, K, options)
return MappedResult(P) as never
}
6 changes: 3 additions & 3 deletions src/type/indexed/indexed-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function FromMappedResult<
T extends TSchema,
R extends TMappedResult
>(T: T, R: R, options: SchemaOptions): TFromMappedResult<T, R> {
return FromProperties(T, R.properties, options) as TFromMappedResult<T, R>
return FromProperties(T, R.properties, options) as never
}
// ------------------------------------------------------------------
// TIndexFromMappedResult
Expand All @@ -87,6 +87,6 @@ export function IndexFromMappedResult<
R extends TMappedResult,
P extends TProperties = TFromMappedResult<T, R>
>(T: T, R: R, options: SchemaOptions): TMappedResult<P> {
const P = FromMappedResult(T, R, options) as unknown as P
return MappedResult(P)
const P = FromMappedResult(T, R, options)
return MappedResult(P) as never
}
6 changes: 3 additions & 3 deletions src/type/indexed/indexed-property-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TFromTemplateLiteral<T extends TTemplateLiteral, R extends string[] = TTemp
// prettier-ignore
function FromTemplateLiteral<T extends TTemplateLiteral>(T: T): TFromTemplateLiteral<T> {
const R = TemplateLiteralGenerate(T) as string[]
return R.map(S => S.toString()) as TFromTemplateLiteral<T>
return R.map(S => S.toString()) as never
}
// ------------------------------------------------------------------
// FromUnion
Expand Down Expand Up @@ -75,7 +75,7 @@ type TFromLiteral<T extends TLiteralValue> = (
function FromLiteral<T extends TLiteralValue>(T: T): TFromLiteral<T> {
return (
[(T as string).toString()] // TS 5.4 observes TLiteralValue as not having a toString()
) as TFromLiteral<T>
) as never
}
// ------------------------------------------------------------------
// IndexedKeyResolve
Expand All @@ -99,5 +99,5 @@ export function IndexPropertyKeys<T extends TSchema>(T: T): TIndexPropertyKeys<T
IsNumber(T) ? ['[number]'] :
IsInteger(T) ? ['[number]'] :
[]
))] as TIndexPropertyKeys<T>
))] as never
}

0 comments on commit 339491e

Please sign in to comment.