| 1 | import {expectType} from 'tsd'; |
| 2 | import type {DelimiterCasedPropertiesDeep} from '../index.d.ts'; |
| 3 | |
| 4 | declare const foo: DelimiterCasedPropertiesDeep<{helloWorld: {fooBar: string}}, '/'>; |
| 5 | expectType<{'hello/world': {'foo/bar': string}}>(foo); |
| 6 | |
| 7 | declare const fooBar: DelimiterCasedPropertiesDeep<() => {a: string}, '/'>; |
| 8 | expectType<() => {a: string}>(fooBar); |
| 9 | |
| 10 | declare const bar: DelimiterCasedPropertiesDeep<Set<{fooBar: string}>, '-'>; |
| 11 | expectType<Set<{'foo-bar': string}>>(bar); |
| 12 | |
| 13 | declare const withOptions: DelimiterCasedPropertiesDeep<Set<{helloWorld: {p2p: Array<{addressLine1: string}>}}>, '.', {splitOnNumbers: true}>; |
| 14 | expectType<Set<{'hello.world': {'p.2.p': Array<{'address.line.1': string}>}}>>(withOptions); |
| 15 | |
| 16 | declare const withPunctuation: DelimiterCasedPropertiesDeep<{'hello@World1': {'foo::Bar': string}}, '.'>; |
| 17 | expectType<{'[email protected]': {'foo::.bar': string}}>(withPunctuation); |
| 18 | |
| 19 | declare const withPunctuationSplit: DelimiterCasedPropertiesDeep<{'hello@World1': {'foo::Bar': string}}, '.', {splitOnPunctuation: true}>; |
| 20 | expectType<{'hello.world1': {'foo.bar': string}}>(withPunctuationSplit); |
| 21 | |
| 22 | declare const withPunctuationSplitAndNumbers: DelimiterCasedPropertiesDeep<{'hello@World1': {'foo::Bar1': string}}, '.', {splitOnPunctuation: true; splitOnNumbers: true}>; |
| 23 | expectType<{'hello.world.1': {'foo.bar.1': string}}>(withPunctuationSplitAndNumbers); |
| 24 | |
| 25 | // Verify example |
| 26 | type User = { |
| 27 | userId: number; |
| 28 | userName: string; |
| 29 | date: Date; |
| 30 | regExp: RegExp; |
| 31 | }; |
| 32 | |
| 33 | type UserWithFriends = { |
| 34 | userInfo: User; |
| 35 | userFriends: User[]; |
| 36 | }; |
| 37 | |
| 38 | type UserPunctuated = { |
| 39 | 'user::id': number; |
| 40 | 'user::name': string; |
| 41 | date: Date; |
| 42 | 'reg::exp': RegExp; |
| 43 | }; |
| 44 | |
| 45 | type UserWithFriendsPunctuated = { |
| 46 | 'user@info': UserPunctuated; |
| 47 | 'user#friends': UserPunctuated[]; |
| 48 | }; |
| 49 | |
| 50 | const result: DelimiterCasedPropertiesDeep<UserWithFriends, '-'> = { |
| 51 | 'user-info': { |
| 52 | 'user-id': 1, |
| 53 | 'user-name': 'Tom', |
| 54 | date: new Date(), |
| 55 | 'reg-exp': /.*/, |
| 56 | }, |
| 57 | 'user-friends': [ |
| 58 | { |
| 59 | 'user-id': 2, |
| 60 | 'user-name': 'Jerry', |
| 61 | date: new Date(), |
| 62 | 'reg-exp': /.*/, |
| 63 | }, |
| 64 | { |
| 65 | 'user-id': 3, |
| 66 | 'user-name': 'Spike', |
| 67 | date: new Date(), |
| 68 | 'reg-exp': /.*/, |
| 69 | }, |
| 70 | ], |
| 71 | }; |
| 72 | expectType<DelimiterCasedPropertiesDeep<UserWithFriends, '-'>>(result); |
| 73 | expectType<DelimiterCasedPropertiesDeep<UserWithFriendsPunctuated, '-', {splitOnPunctuation: true}>>(result); |
| 74 | |
| 75 | // Test object key properties |
| 76 | declare const key: DelimiterCasedPropertiesDeep<{readonly userId?: number}, '-'>; |
| 77 | expectType<{readonly 'user-id'?: number}>(key); |
| 78 | |
| 79 | /** Test Array */ |
| 80 | // Test for tuple |
| 81 | declare const tuple: DelimiterCasedPropertiesDeep<[User], '-'>; |
| 82 | expectType<[{'user-id': number; 'user-name': string; date: Date; 'reg-exp': RegExp}]>(tuple); |
| 83 | declare const tuple2: DelimiterCasedPropertiesDeep<['UserId', 'UserAge', string], '-'>; |
| 84 | expectType<['UserId', 'UserAge', string]>(tuple2); |
| 85 | // Test for readonly tuple |
| 86 | declare const readonlyTuple: DelimiterCasedPropertiesDeep<readonly [{userId: string}, {userName: number}], '-'>; |
| 87 | expectType<readonly [{'user-id': string}, {'user-name': number}]>(readonlyTuple); |
| 88 | // Test for array |
| 89 | declare const array: DelimiterCasedPropertiesDeep<User[], '-'>; |
| 90 | expectType<Array<{'user-id': number; 'user-name': string; date: Date; 'reg-exp': RegExp}>>(array); |
| 91 | // Test for readonly array |
| 92 | declare const readonlyArray: DelimiterCasedPropertiesDeep<ReadonlyArray<{userId: string}>, '-'>; |
| 93 | expectType<ReadonlyArray<{'user-id': string}>>(readonlyArray); |
| 94 | // Test for tailing spread array |
| 95 | declare const tailingSpreadArray: DelimiterCasedPropertiesDeep<[{userId: string}, {userName: number}, ...Array<{userAge: number}>], '-'>; |
| 96 | expectType<[{'user-id': string}, {'user-name': number}, ...Array<{'user-age': number}>]>(tailingSpreadArray); |
| 97 | // Test for leading spread array |
| 98 | declare const leadingSpreadArray: DelimiterCasedPropertiesDeep<[...Array<{userId: string}>, {userName: number}, {userAge: number}], '-'>; |
| 99 | expectType<[...Array<{'user-id': string}>, {'user-name': number}, {'user-age': number}]>(leadingSpreadArray); |
| 100 | // Test for enum |
| 101 | enum UserType { |
| 102 | AdminUser = 'adminUser', |
| 103 | NormalUser = 'normalUser', |
| 104 | } |
| 105 | declare const enumTest: DelimiterCasedPropertiesDeep<{userType: UserType}, '-'>; |
| 106 | expectType<{['user-type']: UserType}>(enumTest); |
| 107 | enumTest['user-type'] = UserType.AdminUser; |
| 108 | |
| 109 | expectType<{'foo-bar': unknown}>({} as DelimiterCasedPropertiesDeep<{fooBar: unknown}, '-'>); |
| 110 | expectType<{'foo_bar': {'bar_baz': unknown}; biz: unknown}>({} as DelimiterCasedPropertiesDeep<{fooBar: {barBaz: unknown}; biz: unknown}, '_'>); |
| 111 | |
| 112 | expectType<{'foo-bar': any}>({} as DelimiterCasedPropertiesDeep<{fooBar: any}, '-'>); |
| 113 | expectType<{'foo_bar': {'bar_baz': any}; biz: any}>({} as DelimiterCasedPropertiesDeep<{fooBar: {barBaz: any}; biz: any}, '_'>); |
| 114 | |
| 115 | expectType<{'foo-bar': unknown}>({} as DelimiterCasedPropertiesDeep<{'foo::bar': unknown}, '-', {splitOnPunctuation: true}>); |
| 116 | expectType<{'foo_bar': {'bar_baz': unknown}; biz: unknown}>({} as DelimiterCasedPropertiesDeep<{'foo::bar': {'bar@baz': unknown}; biz: unknown}, '_', {splitOnPunctuation: true}>); |