2017-02-01 19 views
11

bu arayüze sahiptir:Recursive Kısmi <T> 2.1

export interface UserSettings 
{ 
    one: { 
     three: number; 
     four: number; 
    }; 
    two: { 
     five: number; 
     six: number; 
    }; 
} 

... ve bu çevirmek istiyorum:

export interface UserSettingsForUpdate 
{ 
    one?: { 
     three?: number; 
     four?: number; 
    }; 
    two?: { 
     five?: number; 
     six?: number; 
    }; 
} 

... ama Partial<UserSettings> bu üretir:

{ 
    one?: { 
     three: number; 
     four: number; 
    }; 
    two?: { 
     five: number; 
     six: number; 
    }; 
} 

Tüm derinliklerde isteğe bağlı özelliklerin tümünü yapmak için eşlenen türlerin kullanılması mümkün mü? Bunun için bir arayüz oluşturmalı mıyım?

cevap

11

böyle, kendi haritalama türünü yapabilir:

type RecursivePartial<T> = { 
    [P in keyof T]?: RecursivePartial<T[P]>; 
}; 

enter image description here

Maalesef bu dizi-daktilo alanlar için çalışmaz. Koşullu tür haritalamayı henüz yapmanın bir yolu yoktur; yani ilkellere sınırlama. Bakınız: https://github.com/Microsoft/TypeScript/pull/12114#issuecomment-259776847

+0

Güzel, teşekkürler! Ancak, 'Bar' arayüzünü 'Bar {names: string []; } 'daha sonra VS Kodu,' '' '' '' '' 'dize []' yerine 'RecursivePartial' türüne sahip olduğunu söyler. Düşüncesi olan var mı? –

+0

Bunun geçerli bir sınırlama olduğundan şüpheleniyorum: https://github.com/Microsoft/TypeScript/pull/12114#issuecomment-259776847, https://github.com/Microsoft/TypeScript/issues/12424 ve https: // adresine bakın. github.com/Microsoft/TypeScript/issues/13257 –

+0

Bu sorunları izleyip çözüldüklerinde bu yanıtı güncelleyeceğim. –