mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-05 15:27:05 +00:00
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:
+1
@@ -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' },
|
||||
|
||||
Reference in New Issue
Block a user