Skip to content

Commit 5fef62b

Browse files
authored
Merge pull request #162 from launchdarkly/eb/ch32565/typescript
change TS declarations to prefer interfaces over types
2 parents 3f173ab + 8c2f031 commit 5fef62b

File tree

3 files changed

+102
-17
lines changed

3 files changed

+102
-17
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
11

22
// This file exists only so that we can run the TypeScript compiler in the CI build
33
// to validate our typings.d.ts file.
4+
5+
import * as ld from 'ldclient-js-common';
6+
7+
var ver: string = ld.version;
8+
9+
var logger: ld.LDLogger = ld.createConsoleLogger("info");
10+
var userWithKeyOnly: ld.LDUser = { key: 'user' };
11+
var user: ld.LDUser = {
12+
key: 'user',
13+
name: 'name',
14+
firstName: 'first',
15+
lastName: 'last',
16+
email: 'test@example.com',
17+
avatar: 'http://avatar.url',
18+
ip: '1.1.1.1',
19+
country: 'us',
20+
anonymous: true,
21+
custom: {
22+
'a': 's',
23+
'b': true,
24+
'c': 3,
25+
'd': [ 'x', 'y' ],
26+
'e': [ true, false ],
27+
'f': [ 1, 2 ]
28+
},
29+
privateAttributeNames: [ 'name', 'email' ]
30+
};
31+
32+
var client: ld.LDClientBase = {} as ld.LDClientBase; // wouldn't do this in real life, it's just so the following statements will compile
33+
34+
var boolFlagValue: ld.LDFlagValue = client.variation('key', false);
35+
var numberFlagValue: ld.LDFlagValue = client.variation('key', 2);
36+
var stringFlagValue: ld.LDFlagValue = client.variation('key', 'default');
37+
var jsonFlagValue: ld.LDFlagValue = client.variation('key', [ 'a', 'b' ]);
38+
39+
var detail: ld.LDEvaluationDetail = client.variationDetail('key', 'default');
40+
var detailValue: ld.LDFlagValue = detail.value;
41+
var detailIndex: number | undefined = detail.variationIndex;
42+
var detailReason: ld.LDEvaluationReason = detail.reason;
43+
44+
var flagSet: ld.LDFlagSet = client.allFlags();
45+
var flagSetValue: ld.LDFlagValue = flagSet['key'];

packages/ldclient-js-common/typings.d.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ declare module 'ldclient-js-common' {
1818
/**
1919
* A map of feature flags from their keys to their values.
2020
*/
21-
export type LDFlagSet = {
21+
export interface LDFlagSet {
2222
[key: string]: LDFlagValue;
23-
};
23+
}
2424

2525
/**
2626
* A map of feature flag keys to objects holding changes in their values.
2727
*/
28-
export type LDFlagChangeset = {
28+
export interface LDFlagChangeset {
2929
[key: string]: {
3030
current: LDFlagValue;
3131
previous: LDFlagValue;
3232
};
33-
};
33+
}
3434

3535
/**
3636
* The minimal interface for any object that LDClient can use for logging.
@@ -154,15 +154,16 @@ declare module 'ldclient-js-common' {
154154

155155
/**
156156
* Whether all user attributes (except the user key) should be marked as private, and
157-
* not sent to LaunchDarkly.
157+
* not sent to LaunchDarkly in analytics events.
158158
*
159159
* By default, this is false.
160160
*/
161161
allAttributesPrivate?: boolean;
162162

163163
/**
164164
* The names of user attributes that should be marked as private, and not sent
165-
* to LaunchDarkly.
165+
* to LaunchDarkly in analytics events. You can also specify this on a per-user basis
166+
* with [[LDUser.privateAttributeNames]].
166167
*/
167168
privateAttributeNames?: Array<string>;
168169

@@ -211,7 +212,7 @@ declare module 'ldclient-js-common' {
211212
/**
212213
* A LaunchDarkly user object.
213214
*/
214-
export type LDUser = {
215+
export interface LDUser {
215216
/**
216217
* A unique string identifying a user.
217218
*/
@@ -268,13 +269,21 @@ declare module 'ldclient-js-common' {
268269
custom?: {
269270
[key: string]: string | boolean | number | Array<string | boolean | number>;
270271
};
272+
273+
/**
274+
* Specifies a list of attribute names (either built-in or custom) which should be
275+
* marked as private, and not sent to LaunchDarkly in analytics events. This is in
276+
* addition to any private attributes designated in the global configuration
277+
* with [[LDOptions.privateAttributeNames]] or [[LDOptions.allAttributesPrivate]].
278+
*/
279+
privateAttributeNames?: Array<string>;
271280
}
272281

273282
/**
274283
* Describes the reason that a flag evaluation produced a particular value. This is
275284
* part of the [[LDEvaluationDetail]] object returned by [[LDClient.variationDetail]].
276285
*/
277-
export type LDEvaluationReason = {
286+
export interface LDEvaluationReason {
278287
/**
279288
* The general category of the reason:
280289
*
@@ -308,7 +317,7 @@ declare module 'ldclient-js-common' {
308317
* The key of the failed prerequisite flag, if the kind was `'PREREQUISITE_FAILED'`.
309318
*/
310319
prerequisiteKey?: string;
311-
};
320+
}
312321

313322
/**
314323
* An object that combines the result of a feature flag evaluation with information about
@@ -318,7 +327,7 @@ declare module 'ldclient-js-common' {
318327
*
319328
* For more information, see the [SDK reference guide](https://docs.launchdarkly.com/docs/evaluation-reasons).
320329
*/
321-
export type LDEvaluationDetail = {
330+
export interface LDEvaluationDetail {
322331
/**
323332
* The result of the flag evaluation. This will be either one of the flag's variations or
324333
* the default value that was passed to [[LDClient.variationDetail]].
@@ -335,7 +344,7 @@ declare module 'ldclient-js-common' {
335344
* An object describing the main factor that influenced the flag evaluation value.
336345
*/
337346
reason: LDEvaluationReason;
338-
};
347+
}
339348

340349
/**
341350
* The basic interface for the LaunchDarkly client. The browser SDK and the Electron SDK both

packages/ldclient-js/test-types.ts

+40-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// This file exists only so that we can run the TypeScript compiler in the CI build
33
// to validate our typings.d.ts file.
44

5-
import { createConsoleLogger, LDClient, LDLogger, LDOptions, LDUser, initialize } from 'ldclient-js';
5+
import * as ld from 'ldclient-js';
66

7-
var emptyOptions: LDOptions = {};
8-
var logger: LDLogger = createConsoleLogger("info");
9-
var allOptions: LDOptions = {
7+
var ver: string = ld.version;
8+
9+
var emptyOptions: ld.LDOptions = {};
10+
var logger: ld.LDLogger = ld.createConsoleLogger("info");
11+
var allOptions: ld.LDOptions = {
1012
bootstrap: { },
1113
hash: '',
1214
baseUrl: '',
@@ -27,5 +29,37 @@ var allOptions: LDOptions = {
2729
streamReconnectDelay: 1,
2830
logger: logger
2931
};
30-
var user: LDUser = { key: 'user' };
31-
var client: LDClient = initialize('env', user, allOptions);
32+
var userWithKeyOnly: ld.LDUser = { key: 'user' };
33+
var user: ld.LDUser = {
34+
key: 'user',
35+
name: 'name',
36+
firstName: 'first',
37+
lastName: 'last',
38+
email: 'test@example.com',
39+
avatar: 'http://avatar.url',
40+
ip: '1.1.1.1',
41+
country: 'us',
42+
anonymous: true,
43+
custom: {
44+
'a': 's',
45+
'b': true,
46+
'c': 3,
47+
'd': [ 'x', 'y' ],
48+
'e': [ true, false ],
49+
'f': [ 1, 2 ]
50+
},
51+
privateAttributeNames: [ 'name', 'email' ]
52+
};
53+
var client: ld.LDClient = ld.initialize('env', user, allOptions);
54+
55+
var boolFlagValue: ld.LDFlagValue = client.variation('key', false);
56+
var numberFlagValue: ld.LDFlagValue = client.variation('key', 2);
57+
var stringFlagValue: ld.LDFlagValue = client.variation('key', 'default');
58+
59+
var detail: ld.LDEvaluationDetail = client.variationDetail('key', 'default');
60+
var detailValue: ld.LDFlagValue = detail.value;
61+
var detailIndex: number | undefined = detail.variationIndex;
62+
var detailReason: ld.LDEvaluationReason = detail.reason;
63+
64+
var flagSet: ld.LDFlagSet = client.allFlags();
65+
var flagSetValue: ld.LDFlagValue = flagSet['key'];

0 commit comments

Comments
 (0)