From 038fb00ba8c49afe5ade4748b40f1f9eda763c93 Mon Sep 17 00:00:00 2001 From: harris-miller Date: Sun, 15 Oct 2023 17:25:29 -0600 Subject: [PATCH] tests --- test/propEq.test.ts | 149 ++++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 74 deletions(-) diff --git a/test/propEq.test.ts b/test/propEq.test.ts index 8ae8e89..6a86806 100644 --- a/test/propEq.test.ts +++ b/test/propEq.test.ts @@ -3,85 +3,86 @@ import { expectError, expectType } from 'tsd'; import { propEq } from '../es'; type Obj = { - union: 'foo' | 'bar'; - str: string; - num: number; - u: undefined; - n: null; + literals: 'A' | 'B'; + union: number | string; + nullable: number | null; + optional?: number; }; -const str: string = ''; -// explanation - -// `string` is too wide for `'foo' | 'bar` for assignment -({} as Obj).union = str; -// but for comparison is fine -({} as Obj).union === str; -// null and undefined are allowed as well -({} as Obj).str === null; -({} as Obj).str === undefined; - -// this is why we use `WidenLiterals` in the type definition - -// propEq(val, name, obj) -expectType(propEq('foo', 'union', {} as Obj)); -// any string works here, see the above explanation as to why -expectType(propEq('else', 'union', {} as Obj)); -// also null or undefined -expectType(propEq(null, 'union', {} as Obj)); -expectType(propEq(undefined, 'union', {} as Obj)); -// completely different type fails -expectError(propEq(2, 'union', {} as Obj)); -// other props work as expected -expectType(propEq(2, 'num', {} as Obj)); -expectError(propEq(2, 'u', {} as Obj)); -expectType(propEq(2, 'num', {} as Obj)); -expectError(propEq(2, 'n', {} as Obj)); -expectType(propEq(null, 'n', {} as Obj)); -expectError(propEq(2, 'n', {} as Obj)); -expectType(propEq(undefined, 'u', {} as Obj)); +const obj = {} as Obj; // propEq(val)(name)(obj) -expectType(propEq('foo')('union')({} as Obj)); -// any string works here, see the above explanation as to why -expectType(propEq('else')('union')({} as Obj)); -// completely different type fails -expectError(propEq(2, 'union', {} as Obj)); -// other props work as expected -expectType(propEq(2, 'num', {} as Obj)); -expectError(propEq(2)('u')({} as Obj)); -expectType(propEq(2)('num')({} as Obj)); -expectError(propEq(2)('n')({} as Obj)); -expectType(propEq(null, 'n', {} as Obj)); -expectError(propEq(2)('n')({} as Obj)); -expectType(propEq(undefined, 'u', {} as Obj)); +expectType(propEq('A')('literals')(obj)); +// any string works here and will return boolean, we can live with that +expectType(propEq('C')('literals')(obj)); +// a different base type returns `never` +expectType(propEq(2)('literals')(obj)); +// unions work +expectType(propEq(2)('union')(obj)); +expectType(propEq('2')('union')(obj)); +expectType(propEq(true)('union')(obj)); +// nullable works +expectType(propEq(2)('nullable')(obj)); +expectType(propEq(null)('nullable')(obj)); +// optionals work +expectType(propEq(2)('optional')(obj)); +expectType(propEq(undefined)('optional')(obj)); +// unknownKey errors +expectError(propEq('whatever')('unknownKey')(obj)); // propEq(val)(name, obj) -expectType(propEq('foo')('union', {} as Obj)); -// 'nope' is inferred as 'string' here. -expectType(propEq('nope')('union', {} as Obj)); -// completely different type fails -expectError(propEq(2)('union', {} as Obj)); -// other props work as expected -expectType(propEq(2)('num', {} as Obj)); -expectError(propEq(2)('u', {} as Obj)); -expectType(propEq(2)('num', {} as Obj)); -expectError(propEq(2)('n', {} as Obj)); -expectType(propEq(null)('n', {} as Obj)); -expectError(propEq(2)('n', {} as Obj)); -expectType(propEq(undefined, 'u', {} as Obj)); +expectType(propEq('A')('literals', obj)); +// any string works here and will return boolean, we can live with that +expectType(propEq('C')('literals', obj)); +// a different base type returns `never` +expectType(propEq(2)('literals', obj)); +// unions work +expectType(propEq(2)('union', obj)); +expectType(propEq('2')('union', obj)); +expectType(propEq(true)('union', obj)); +// nullable works +expectType(propEq(2)('nullable', obj)); +expectType(propEq(null)('nullable', obj)); +// optionals work +expectType(propEq(2)('optional', obj)); +expectType(propEq(undefined)('optional', obj)); +// unknownKey errors +expectError(propEq('whatever')('unknownKey', obj)); // propEq(val, name)(obj) -expectType(propEq('foo', 'union')({} as Obj)); -// any string works here, see the above explanation as to why -expectType(propEq('else', 'union')({} as Obj)); -// completely different type fails -expectError(propEq(2, 'union')({} as Obj)); -// other props work as expected -expectType(propEq(2, 'num')({} as Obj)); -expectError(propEq(2, 'u')({} as Obj)); -expectType(propEq(2, 'num')({} as Obj)); -expectError(propEq(2, 'n')({} as Obj)); -expectType(propEq(null, 'n')({} as Obj)); -expectError(propEq(2, 'n')({} as Obj)); -expectType(propEq(undefined, 'u')({} as Obj)); +expectType(propEq('A', 'literals')(obj)); +// any string works here and will return boolean, we can live with that +expectType(propEq('C', 'literals')(obj)); +// a different base type returns `never` +expectType(propEq(2, 'literals')(obj)); +// unions work +expectType(propEq(2, 'union')(obj)); +expectType(propEq('2', 'union')(obj)); +expectType(propEq(true, 'union')(obj)); +// nullable works +expectType(propEq(2, 'nullable')(obj)); +expectType(propEq(null, 'nullable')(obj)); +// optionals work +expectType(propEq(2, 'optional')(obj)); +expectType(propEq(undefined, 'optional')(obj)); +// unknownKey errors +expectError(propEq('whatever', 'unknownKey')(obj)); + +// propEq(val, name, obj) +expectType(propEq('A', 'literals', obj)); +// any string works here and will return boolean, we can live with that +expectError(propEq('C', 'literals', obj)); +// a different base type returns `never` +expectError(propEq(2, 'literals', obj)); +// unions work +expectType(propEq(2, 'union', obj)); +expectType(propEq('2', 'union', obj)); +expectError(propEq(true, 'union', obj)); +// nullable works +expectType(propEq(2, 'nullable', obj)); +expectType(propEq(null, 'nullable', obj)); +// optionals work +expectType(propEq(2, 'optional', obj)); +expectType(propEq(undefined, 'optional', obj)); +// unknownKey errors +expectError(propEq('whatever', 'unknownKey', obj));