|
| 1 | +import {expectType} from 'tsd-check'; |
| 2 | +import writeJsonFile, {sync, Replacer, SortKeys, JSONStringifyable} from '.'; |
| 3 | + |
| 4 | +const replacer: Replacer = (key: string) => { |
| 5 | + if (key === 'unicorn') { |
| 6 | + return {rainbow: '🌈'}; |
| 7 | + } else if (key === 'rainbow') { |
| 8 | + return () => 'foo'; |
| 9 | + } |
| 10 | + |
| 11 | + return key.toUpperCase(); |
| 12 | +}; |
| 13 | + |
| 14 | +const sortKeys: SortKeys = (a: string, b: string) => a.length - b.length; |
| 15 | + |
| 16 | +(async () => { |
| 17 | + expectType<JSONStringifyable>('🦄'); |
| 18 | + expectType<JSONStringifyable>(1); |
| 19 | + expectType<JSONStringifyable>(true); |
| 20 | + expectType<JSONStringifyable>(new Date()); |
| 21 | + expectType<JSONStringifyable>(['hello', 'world']); |
| 22 | + expectType<JSONStringifyable>({unicorn: '🦄'}); |
| 23 | + |
| 24 | + expectType<SortKeys>(() => 1); |
| 25 | + expectType<SortKeys>((a: string) => a.length); |
| 26 | + expectType<SortKeys>((a: string, b: string) => a.length - b.length); |
| 27 | + |
| 28 | + expectType<Replacer>(() => 1); |
| 29 | + expectType<Replacer>(() => 'unicorn'); |
| 30 | + expectType<Replacer>(() => true); |
| 31 | + expectType<Replacer>(() => null); |
| 32 | + expectType<Replacer>(() => undefined); |
| 33 | + expectType<Replacer>(() => ({unicorn: '🦄'})); |
| 34 | + expectType<Replacer>(() => () => 'foo'); |
| 35 | + expectType<Replacer>((key: string) => key.toUpperCase()); |
| 36 | + |
| 37 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'})); |
| 38 | + expectType<void>(await writeJsonFile('unicorn.json', '🦄')); |
| 39 | + expectType<void>(await writeJsonFile('date.json', new Date())); |
| 40 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {detectIndent: true})); |
| 41 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: ' '})); |
| 42 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {indent: 4})); |
| 43 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {mode: 0o666})); |
| 44 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer})); |
| 45 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]})); |
| 46 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys})); |
| 47 | + expectType<void>(await writeJsonFile('unicorn.json', {unicorn: '🦄'}, {sortKeys: true})); |
| 48 | + |
| 49 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'})); |
| 50 | + expectType<void>(sync('unicorn.json', '🦄')); |
| 51 | + expectType<void>(sync('date.json', new Date())); |
| 52 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {detectIndent: true})); |
| 53 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {indent: ' '})); |
| 54 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {indent: 4})); |
| 55 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {mode: 0o666})); |
| 56 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {replacer})); |
| 57 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {replacer: ['unicorn', 1]})); |
| 58 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys})); |
| 59 | + expectType<void>(sync('unicorn.json', {unicorn: '🦄'}, {sortKeys: true})); |
| 60 | +})(); |
0 commit comments