Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript 2.1: use keyof #1

Open
wstaelens opened this issue Nov 28, 2016 · 4 comments
Open

TypeScript 2.1: use keyof #1

wstaelens opened this issue Nov 28, 2016 · 4 comments

Comments

@wstaelens
Copy link
Owner

See:

change the options to the new keyof.
snippet from stackoverflow post: http://stackoverflow.com/a/40843364/187650

type toolbarOptionsMap = {
    'style': 'bold' | 'italic' | 'underline',
    'font': 'strikethrough' | 'superscript' | 'subscript'
    ...
}

type toolbarOption<T extends keyof toolbarOptionsMap> = [T, toolbarOptionsMap[T][]];
// keyof toolbarOptionsMap = 'style' | 'font'
// T extends keyof toolbarOptionsMap, so is one of those strings.
// toolbarOptionsMap[T] is the type of the corresponding value for T

// Then we just make a list of these
type toolbar = toolbarOption<any>[];
@wstaelens
Copy link
Owner Author

@pimterry
Copy link

pimterry commented Dec 9, 2016

You mentioned on your SO question that you've attempted to apply this to your existing code, and it's not working. So that I can help, can you show me some code that's using keyof as above, but which doesn't compile?

@wstaelens
Copy link
Owner Author

Currently, Out of office but will try to give more details soon.
Basically i've commented out this code below and the toolbar option and added the code from SO.

type toolbarStyleGroupOptions = 'style' | 'bold' | 'italic' | 'underline' | 'clear';
type toolbarFontGroupOptions = 'strikethrough' | 'superscript' | 'subscript';
type toolbarFontsizeGroupOptions = 'fontsize';
type toolbarColorGroupOptions = 'color';
type toolbarParaGroupOptions = 'ul' | 'ol' | 'paragraph';
type toolbarHeightGroupOptions = 'height';
type toolbarTableGroupOptions = 'table';
type toolbarInsertGroupOptions = 'link' | 'picture' | 'hr';
type toolbarViewGroupOptions = 'fullscreen' | 'codeview';
type toolbarHelpGroupOptions = 'help';
//type toolbarDef = [string, string[]][];
type toolbarDef = [
    ['style', toolbarStyleGroupOptions[]]
    | ['font', toolbarFontGroupOptions[]]
    | ['fontsize', toolbarFontsizeGroupOptions[]]
    | ['color', toolbarColorGroupOptions[]]
    | ['para', toolbarParaGroupOptions[]]
    | ['height', toolbarHeightGroupOptions[]]
    | ['table', toolbarTableGroupOptions[]]
    | ['insert', toolbarInsertGroupOptions[]]
    | ['view', toolbarViewGroupOptions[]]
    | ['help', toolbarHelpGroupOptions[]]
];

@wstaelens
Copy link
Owner Author

interface SummernoteOptions {
    toolbar?: toolbar;
}

type toolbarOptionsMap = {
    'style': 'style' | 'bold' | 'italic' | 'underline' | 'clear',
    'font': 'strikethrough' | 'superscript' | 'subscript',
    'fontsize': 'fontsize',
    'color': 'color',
    'para': 'ul' | 'ol' | 'paragraph',
    'height': 'height',
    'table': 'table',
    'insert': 'link' | 'picture' | 'hr',
    'view': 'fullscreen' | 'codeview',
    'help': 'help'
};

type toolbarOption<T extends keyof toolbarOptionsMap> = [T, toolbarOptionsMap[T][]];
type toolbar = toolbarOption<keyof toolbarOptionsMap>[];

"Type 'toolbarOptionsMap' does not satisfy the constraint "style" | "font" | "fontsize" | .... | "help".

@pimterry this was the part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants