/** * The purpose of these tests is to make sure the types are exposed how we expect, * and that we double-check what we're exposing as public API */ import {expectTypeOf} from 'expect-type'; import {Signal} from './wrapper.ts'; /** * Top-Level */ expectTypeOf().toEqualTypeOf< 'State' | 'Computed' | 'subtle' | 'isState' | 'isComputed' | 'isWatcher' >(); /** * Construction works as expected */ expectTypeOf(Signal.State).toBeConstructibleWith(1); expectTypeOf(Signal.State).toBeConstructibleWith(1, {}); expectTypeOf(Signal.State).toBeConstructibleWith(1, {equals: (a, b) => true}); expectTypeOf(Signal.State).toBeConstructibleWith(1, {[Signal.subtle.watched]: () => true}); expectTypeOf(Signal.State).toBeConstructibleWith(1, { [Signal.subtle.unwatched]: () => true, }); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 2); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 1, {equals: (a, b) => true}); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 1, { [Signal.subtle.watched]: () => true, }); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 1, { [Signal.subtle.unwatched]: () => true, }); // @ts-expect-error expectTypeOf>().toBeConstructibleWith(); // @ts-expect-error expectTypeOf(Signal.State).toBeConstructibleWith('wrong', {}); expectTypeOf(Signal.State).toBeConstructibleWith(1, { // @ts-expect-error [Signal.subtle.watched]: 2, }); expectTypeOf(Signal.State).toBeConstructibleWith(1, { // @ts-expect-error [Signal.subtle.unwatched]: 2, }); expectTypeOf(Signal.State).toBeConstructibleWith(1, { // @ts-expect-error typo: (a, b) => true, }); // @ts-expect-error expectTypeOf>().toBeConstructibleWith(); // @ts-expect-error expectTypeOf(Signal.Computed).toBeConstructibleWith('wrong'); // @ts-expect-error expectTypeOf(Signal.Computed).toBeConstructibleWith(2); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 1, { // @ts-expect-error [Signal.subtle.watched]: 2, }); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 1, { // @ts-expect-error [Signal.subtle.unwatched]: 2, }); expectTypeOf(Signal.Computed).toBeConstructibleWith(() => 1, { // @ts-expect-error typo: (a, b) => true, }); /** * Properties on each of the instances / namespaces */ expectTypeOf & string>().toEqualTypeOf<'get' | 'set'>(); expectTypeOf & string>().toEqualTypeOf<'get'>(); expectTypeOf().toEqualTypeOf< | 'untrack' | 'currentComputed' | 'introspectSources' | 'introspectSinks' | 'hasSinks' | 'hasSources' | 'Watcher' | 'watched' | 'unwatched' >(); expectTypeOf().toEqualTypeOf< 'watch' | 'unwatch' | 'getPending' >(); /** * Inference works */ expectTypeOf(new Signal.State(0)).toEqualTypeOf>(); expectTypeOf(new Signal.State(0).get()).toEqualTypeOf(); expectTypeOf(new Signal.State(0).set(1)).toEqualTypeOf(); /** * Assigning subtypes works */ expectTypeOf>().toMatchTypeOf>(); expectTypeOf>().toMatchTypeOf>(); /** * Test data types */ interface Broader { strProp: string; } interface Narrower extends Broader { numProp: number; }