Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed Jan 3, 2024
1 parent fdc0c68 commit 038fb00
Showing 1 changed file with 75 additions and 74 deletions.
149 changes: 75 additions & 74 deletions test/propEq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>(propEq('foo', 'union', {} as Obj));
// any string works here, see the above explanation as to why
expectType<boolean>(propEq('else', 'union', {} as Obj));
// also null or undefined
expectType<boolean>(propEq(null, 'union', {} as Obj));
expectType<boolean>(propEq(undefined, 'union', {} as Obj));
// completely different type fails
expectError(propEq(2, 'union', {} as Obj));
// other props work as expected
expectType<boolean>(propEq(2, 'num', {} as Obj));
expectError(propEq(2, 'u', {} as Obj));
expectType<boolean>(propEq(2, 'num', {} as Obj));
expectError(propEq(2, 'n', {} as Obj));
expectType<boolean>(propEq(null, 'n', {} as Obj));
expectError(propEq(2, 'n', {} as Obj));
expectType<boolean>(propEq(undefined, 'u', {} as Obj));
const obj = {} as Obj;

// propEq(val)(name)(obj)
expectType<boolean>(propEq('foo')('union')({} as Obj));
// any string works here, see the above explanation as to why
expectType<boolean>(propEq('else')('union')({} as Obj));
// completely different type fails
expectError(propEq(2, 'union', {} as Obj));
// other props work as expected
expectType<boolean>(propEq(2, 'num', {} as Obj));
expectError(propEq(2)('u')({} as Obj));
expectType<boolean>(propEq(2)('num')({} as Obj));
expectError(propEq(2)('n')({} as Obj));
expectType<boolean>(propEq(null, 'n', {} as Obj));
expectError(propEq(2)('n')({} as Obj));
expectType<boolean>(propEq(undefined, 'u', {} as Obj));
expectType<boolean>(propEq('A')('literals')(obj));
// any string works here and will return boolean, we can live with that
expectType<boolean>(propEq('C')('literals')(obj));
// a different base type returns `never`
expectType<never>(propEq(2)('literals')(obj));
// unions work
expectType<boolean>(propEq(2)('union')(obj));
expectType<boolean>(propEq('2')('union')(obj));
expectType<never>(propEq(true)('union')(obj));
// nullable works
expectType<boolean>(propEq(2)('nullable')(obj));
expectType<boolean>(propEq(null)('nullable')(obj));
// optionals work
expectType<boolean>(propEq(2)('optional')(obj));
expectType<boolean>(propEq(undefined)('optional')(obj));
// unknownKey errors
expectError(propEq('whatever')('unknownKey')(obj));

// propEq(val)(name, obj)
expectType<boolean>(propEq('foo')('union', {} as Obj));
// 'nope' is inferred as 'string' here.
expectType<boolean>(propEq('nope')('union', {} as Obj));
// completely different type fails
expectError(propEq(2)('union', {} as Obj));
// other props work as expected
expectType<boolean>(propEq(2)('num', {} as Obj));
expectError(propEq(2)('u', {} as Obj));
expectType<boolean>(propEq(2)('num', {} as Obj));
expectError(propEq(2)('n', {} as Obj));
expectType<boolean>(propEq(null)('n', {} as Obj));
expectError(propEq(2)('n', {} as Obj));
expectType<boolean>(propEq(undefined, 'u', {} as Obj));
expectType<boolean>(propEq('A')('literals', obj));
// any string works here and will return boolean, we can live with that
expectType<boolean>(propEq('C')('literals', obj));
// a different base type returns `never`
expectType<never>(propEq(2)('literals', obj));
// unions work
expectType<boolean>(propEq(2)('union', obj));
expectType<boolean>(propEq('2')('union', obj));
expectType<never>(propEq(true)('union', obj));
// nullable works
expectType<boolean>(propEq(2)('nullable', obj));
expectType<boolean>(propEq(null)('nullable', obj));
// optionals work
expectType<boolean>(propEq(2)('optional', obj));
expectType<boolean>(propEq(undefined)('optional', obj));
// unknownKey errors
expectError(propEq('whatever')('unknownKey', obj));

// propEq(val, name)(obj)
expectType<boolean>(propEq('foo', 'union')({} as Obj));
// any string works here, see the above explanation as to why
expectType<boolean>(propEq('else', 'union')({} as Obj));
// completely different type fails
expectError(propEq(2, 'union')({} as Obj));
// other props work as expected
expectType<boolean>(propEq(2, 'num')({} as Obj));
expectError(propEq(2, 'u')({} as Obj));
expectType<boolean>(propEq(2, 'num')({} as Obj));
expectError(propEq(2, 'n')({} as Obj));
expectType<boolean>(propEq(null, 'n')({} as Obj));
expectError(propEq(2, 'n')({} as Obj));
expectType<boolean>(propEq(undefined, 'u')({} as Obj));
expectType<boolean>(propEq('A', 'literals')(obj));
// any string works here and will return boolean, we can live with that
expectType<boolean>(propEq('C', 'literals')(obj));
// a different base type returns `never`
expectType<never>(propEq(2, 'literals')(obj));
// unions work
expectType<boolean>(propEq(2, 'union')(obj));
expectType<boolean>(propEq('2', 'union')(obj));
expectType<never>(propEq(true, 'union')(obj));
// nullable works
expectType<boolean>(propEq(2, 'nullable')(obj));
expectType<boolean>(propEq(null, 'nullable')(obj));
// optionals work
expectType<boolean>(propEq(2, 'optional')(obj));
expectType<boolean>(propEq(undefined, 'optional')(obj));
// unknownKey errors
expectError(propEq('whatever', 'unknownKey')(obj));

// propEq(val, name, obj)
expectType<boolean>(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<boolean>(propEq(2, 'union', obj));
expectType<boolean>(propEq('2', 'union', obj));
expectError(propEq(true, 'union', obj));
// nullable works
expectType<boolean>(propEq(2, 'nullable', obj));
expectType<boolean>(propEq(null, 'nullable', obj));
// optionals work
expectType<boolean>(propEq(2, 'optional', obj));
expectType<boolean>(propEq(undefined, 'optional', obj));
// unknownKey errors
expectError(propEq('whatever', 'unknownKey', obj));

0 comments on commit 038fb00

Please sign in to comment.