You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But right after TS implement them we can use it to check and generate better refinement functions. I've written a small PoC to test this possibility and it works really well.
PoC source code
typeNot<T,U>=TextendsU ? never : T;typePrefixOperators<T,A>={'not': Not<T,A>;'nullish': null|void|undefined|A;};typePrefixOperatorName=keyofPrefixOperators<any,any>;typeInfixOperators<_T,A,B>={'or': A|B;'and': A&B;};typeInfixOperatorName=keyofInfixOperators<any,any,any>;typePredicates={'binary': 0|1;'nullish': null|void|undefined;'string': string;'number': number;'bigint': bigint;'boolean': boolean;'symbol': symbol;'undefined': undefined;'object': null|object;'function': (...args: unknown[])=>unknown;};typePredicateName=keyofPredicates;typeEvalType<Eextendsstring,T>=(EextendsPredicateName
? Predicates[E]
: Eextends `${PrefixOperatorName} ${infer A}`
? Eextends `${infer O} ${A}`
? OextendsPrefixOperatorName
? PrefixOperators<T,EvalType<A,T>>[O]
: never
: never
: Eextends `${infer A} ${InfixOperatorName} ${infer B}`
? Eextends `${A} ${infer O} ${B}`
? OextendsInfixOperatorName
? InfixOperators<T,EvalType<A,T>,EvalType<B,T>>[O]
: never
: never
: never);typeEval<Eextendsstring>=(EextendsPredicateName
? ((value: unknown)=>value is EvalType<E,unknown>)
: Eextends `${PrefixOperatorName} ${string}`
? (<T=unknown>(value: T)=>value is EvalType<E,T>)
: Eextends `${string} ${InfixOperatorName} ${string}`
? (<T=unknown>(value: T)=>value is EvalType<E,T>)
: never);declarefunctionis<Eextendsstring>(expression: E): Eval<E>;// ---// USAGE EXAMPLE// ---constisObj=is('object and not nullish');letuser: undefined|null|{name: string;};if(isObj(user)){// 'user' here is an object with name property.console.log(user.name.toUpperCase());}else{// @ts-expect-error because because 'user' here is null or undefined.console.log(user.name.toUpperCase());}
The text was updated successfully, but these errors were encountered:
There's no way to use tagged template string with template literal types:
microsoft/TypeScript#33304
But right after TS implement them we can use it to check and generate better refinement functions. I've written a small PoC to test this possibility and it works really well.
PoC source code
The text was updated successfully, but these errors were encountered: