Skip to content

Instantly share code, notes, and snippets.

@myuon
Last active January 31, 2020 14:04
Show Gist options
  • Save myuon/c583a4d94a0267fbf8b316028c9c3a35 to your computer and use it in GitHub Desktop.
Save myuon/c583a4d94a0267fbf8b316028c9c3a35 to your computer and use it in GitHub Desktop.
type D<T> = {
[P in keyof T]?: T[P] extends Array<infer U> ? Array<D<U>> : T[P]
}
type W<V> = {
wrapper: V
}
type A = D<{
hoge: number,
foo: W<{ bar: string }[]>,
}>
/*
Property 'wrapper' is missing in type 'never[]' but required in type 'W<{ bar: string; }[]>'.(2741)
input.ts(6, 5): 'wrapper' is declared here.
input.ts(11, 5): The expected type comes from property 'foo' which is declared here on type 'D<{ hoge: number; foo: W<{ bar: string; }[]>; }>'
*/
const a: A = {
foo: []
}
@myuon
Copy link
Author

myuon commented Jan 31, 2020

Dつけたら動いた

type D<T> = {
    [P in keyof T]?: T[P] extends Array<infer U> ? Array<D<U>> : D<T[P]>
}

type W<V> = {
    wrapper: V
}

type A = D<{
    hoge: number,
    foo: W<{ bar: string }[]>,
}>

const a: A = {
    foo: {
        wrapper: [{ bar: '' }, {}]
    }
}

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