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

Mapped types, trouble with array member? #15598

Closed
JasonKleban opened this issue May 5, 2017 · 3 comments
Closed

Mapped types, trouble with array member? #15598

JasonKleban opened this issue May 5, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@JasonKleban
Copy link

I'm thinking this is a bug. The intention here is to create a parallel nested type. I'm having trouble with the array member here - there are some type restrictions being enforced, but not enough. myValidation.c shouldn't be allowed to be an array at all, let alone an array of string, but it seems to allow an array of any type there.

interface ValidationRule<T, RootState extends object> {
    isValid: (value: T, state?: RootState) => boolean
}

type Validated<State extends object, Root extends object = State> = {
    [Member in keyof State]:
        Validated<State[Member], Root>
        | ValidationRule<State[Member], Root>;
}

interface AppState {
    a: number,
    b: string,
    c: number[]
}

var myAppState: AppState = {
    a: 5,
    b: "Hello",
    c: [7, 11]
}

var myValidation: Validated<AppState> = {
    a: {
        isValid: (v, s) => true
    },
    b: {
        isValid: (v) => true
    },
    // c: {} //does error, good
    // c: "" //does error, good
    c: ["Why", "doesn't", "this", "error"] // <-- should error
}

Playground link

Also, is there a way to see the mapped type in its expanded form?

@ghost
Copy link

ghost commented May 5, 2017

Probably has to do with the recursion in Validated. Here's a simplified example:

type Validated<State extends object> = {
    [Member in keyof State]: Validated<State[Member]>;
}
const c: Validated<number[]> = ["why", "doesn't", "this", "error"];

@mhegazy
Copy link
Contributor

mhegazy commented May 5, 2017

looks like a duplicate of #12769

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 5, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed May 22, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants