diff --git a/test/pluck.test.ts b/test/pluck.test.ts index 8971c3b..777dadc 100644 --- a/test/pluck.test.ts +++ b/test/pluck.test.ts @@ -3,108 +3,86 @@ import { expectType, expectError } from 'tsd'; import { __, pluck } from '../es'; type Obj = { name: string; age: number }; - -const record = { - a: {name: 'foo', age: 13}, - b: {name: 'bar', age: 31, desc: 'barrr'} +type Rec = { + a: {name: string, age: number}, + b: {name: string, age: number, desc: string} }; -const constRecord = { - a: {name: 'foo', age: 13}, - b: {name: 'bar', age: 31, desc: 'barrr'} -} as const; -const incorrectRecord = { - a: {name: 'foo', age: 13}, - b: {name: 'bar', age: 31, desc: 'barrr'}, - c: {} +type ConstRec = { + readonly a: {name: 'foo', age: 13}, + readonly b: {name: 'bar', age: 31, desc: 'barrr'} }; // pluck(key) -{ - const getFirstItems = pluck(0); - const getName = pluck('name'); - const getAge = pluck('age'); - const getNope = pluck('nope'); - - // pluck(key)(list::Array[]) - { - expectType(getFirstItems([] as string[][])); - expectType(getFirstItems([] as number[][])); - expectType<1[]>(getFirstItems([] as 1[][])); - expectType(getFirstItems([] as Obj[][])); - expectError(getFirstItems('string')); // works in JS, but should not be valid in TS - expectError(getFirstItems({} as Obj)); - } - - // pluck(key)(list::Record[]) - { - expectType(getName([] as Obj[])); - expectType(getAge([] as Obj[])); - expectError(getNope([] as Obj[])); - expectError(getFirstItems([] as Obj[])); - } - - // pluck(key)(record::Record) - { - expectType<{ a: string, b: string }>(getName(record)); - expectType<{ a: number, b: number }>(getAge(record)); - expectType<{ readonly a: 'foo', readonly b: 'bar' }>(getName(constRecord)); - expectError(getNope(record)); - expectError(getAge(incorrectRecord)); - // expectType<{ readonly a: undefined, readonly b: 'barrr' }>(pluck('desc')(constRecord)); - // this ^ gives false negative result, but it can't be fixed right now - } -} +const getFirstItems = pluck(0); +const getName = pluck('name'); +const getAge = pluck('age'); +const getNope = pluck('nope'); + +// pluck(key)(list::Array[]) +expectType(getFirstItems([] as string[][])); +expectType(getFirstItems([] as number[][])); +expectType<1[]>(getFirstItems([] as 1[][])); +expectType(getFirstItems([] as Obj[][])); +expectError(getFirstItems('string')); // works in JS, but should not be valid in TS +expectError(getFirstItems({} as Obj)); + + +// pluck(key)(list::Record[]) +expectType(getName([] as Obj[])); +expectType(getAge([] as Obj[])); +expectError(getNope([] as Obj[])); +expectError(getFirstItems([] as Obj[])); + + +// pluck(key)(record::Record) +expectType<{ a: string, b: string }>(getName({} as Rec)); +expectType<{ a: number, b: number }>(getAge({} as Rec)); +expectType<{ readonly a: 'foo', readonly b: 'bar' }>(getName({} as ConstRec)); +expectError(getNope({} as Rec)); +expectError(pluck('desc')({} as ConstRec)); // works in JS, but not valid in TS + // pluck(key, list::Record[]) -{ - expectType(pluck('name', [] as Obj[])); - expectType(pluck('age', [] as Obj[])); - expectError(pluck('nope', [] as Obj[])); - expectError(pluck(0, [] as Obj[])); -} +expectType(pluck('name', [] as Obj[])); +expectType(pluck('age', [] as Obj[])); +expectError(pluck('nope', [] as Obj[])); +expectError(pluck(0, [] as Obj[])); + // pluck(key, list::Array[]) -{ - expectType(pluck(0, [] as string[][])); - expectType(pluck(1, [] as number[][])); - expectType<1[]>(pluck(0, [] as 1[][])); - expectType(pluck(0, [] as Obj[][])); - expectError(pluck(0, 'string')); // works in JS, but should not be valid in TS - expectError(pluck(0, {} as Obj)); -} +expectType(pluck(0, [] as string[][])); +expectType(pluck(1, [] as number[][])); +expectType<1[]>(pluck(0, [] as 1[][])); +expectType(pluck(0, [] as Obj[][])); +expectError(pluck(0, 'string')); // works in JS, but should not be valid in TS +expectError(pluck(0, {} as Obj)); + // pluck(key, record::Record) -{ - expectType<{ a: string, b: string }>(pluck('name', record)); - expectType<{ readonly a: 'foo', readonly b: 'bar' }>(pluck('name', constRecord)); - expectType<{ a: number, b: number }>(pluck('age', record)); - expectError(pluck(1, record)); - expectError(pluck('nope', record)); - expectError(pluck('age', incorrectRecord)); - // expectType<{ readonly a: undefined, readonly b: 'barrr' }>(pluck('desc', constRecord)); - // this ^ gives false negative result, but it can't be fixed right now -} +expectType<{ a: string, b: string }>(pluck('name', {} as Rec)); +expectType<{ readonly a: 'foo', readonly b: 'bar' }>(pluck('name', {} as ConstRec)); +expectType<{ a: number, b: number }>(pluck('age', {} as Rec)); +expectError(pluck(1, {} as Rec)); +expectError(pluck('nope', {} as Rec)); +expectError(pluck('desc', {} as ConstRec)); // works in JS, but not valid in TS + // pluck(__, list::Record[])(prop) -{ - const getFromObjList = pluck(__, [] as Obj[]); +const getFromObjList = pluck(__, [] as Obj[]); + +expectType(getFromObjList('name')); +expectType(getFromObjList('age')); +expectError(getFromObjList('nope')); - expectType(getFromObjList('name')); - expectType(getFromObjList('age')); - expectError(getFromObjList('nope')); -} // pluck(__, list::Array[])(prop) -{ - expectType(pluck(__, [] as string[][])(0)); - expectType(pluck(__, [] as number[][])(1)); - expectError(pluck(__, [] as Obj[])(0)); -} +expectType(pluck(__, [] as string[][])(0)); +expectType(pluck(__, [] as number[][])(1)); +expectError(pluck(__, [] as Obj[])(0)); + // pluck(__, record::Record)(prop) -{ - expectType<{ a: string, b: string }>(pluck(__, record)('name')); - expectType<{ a: number, b: number }>(pluck(__, record)('age')); - expectError(pluck(__, record)('nope')); - expectError(pluck(__, incorrectRecord)('age')); -} +expectType<{ a: string, b: string }>(pluck(__, {} as Rec)('name')); +expectType<{ a: number, b: number }>(pluck(__, {} as Rec)('age')); +expectError(pluck(__, {} as Rec)('nope')); +