keep element type when string literal array passed

When select options is an array of string literal union type, select must return the same type of value.
This commit is contained in:
Alan Choi
2019-03-07 10:19:19 +09:00
parent a7836521cc
commit 44c10e7262
2 changed files with 10 additions and 1 deletions
+1
View File
@@ -52,6 +52,7 @@ export function object<T>(name: string, value: T, groupId?: string): T;
export function radios<T>(name: string, options: { [s: string]: T }, value?: T, groupId?: string): string;
export function select<T>(name: string, options: { [s: string]: T }, value: T, groupId?: string): T;
export function select<T extends string>(name: string, options: ReadonlyArray<T>, value: T, groupId?: string): T;
export function select(name: string, options: ReadonlyArray<string>, value: string, groupId?: string): string;
export function date(name: string, value?: Date, groupId?: string): Date;
@@ -94,7 +94,15 @@ stories.add('dynamic knobs', () => {
});
const readonlyOptionsArray: ReadonlyArray<string> = ['hi'];
select('With readonly array', readonlyOptionsArray, readonlyOptionsArray[0]);
select('With readonly string array', readonlyOptionsArray, readonlyOptionsArray[0]);
type StringLiteralType = 'Apple' | 'Banana' | 'Grapes';
const stringLiteralArray: StringLiteralType[] = ['Apple', 'Banana', 'Grapes'];
let selectedFruit: StringLiteralType;
// type of value returned from `select` must be `StringLiteralType`.
selectedFruit = select('With string literal array', stringLiteralArray, stringLiteralArray[0]);
const optionsObject = {
Apple: { taste: 'sweet', color: 'red' },