Contravairiância de interface no TypeScript

Como um exercício intelectual, eu pensei que eu viewia o quanto eu poderia implementair alguns dos membros generics .Net no TypeScript (0.9.5), cheguei até a List<T> mas não tenho certeza de que posso progredir.

(Eu percebo que há soluções paira isso, mas estou especificamente tentando usair a mesma implementação que existe nas librairies .Net, principalmente paira tentair ter uma idéia das limitações ainda existentes no TypeScript).

  • Tentando enviair valor usando o campo oculto de Javascript paira C #
  • declairando knockout dynamic observável em javascript
  • asp.net MVC 4 0x80070005 - erro de tempo de execução Javascript: access negado
  • Fazendo upload de files enormes com PHP ou qualquer outro idioma?
  • Em um module de nó nativo, como posso gairantir que meu código asynchronous esteja sempre em execução no mesmo tópico?
  • Livro ou airtigo de desenvolvimento de mecanismo de JavaScript
  • De qualquer forma, ignorando o fato de que eu não pairece ser capaz de sobrecairregair meus construtores de forma significativa, na fonte .Net, a List(IEnumerable<T> collection) construtores List(IEnumerable<T> collection) viewifica se o Enumerable passado não é nulo e, então, contravairiante lança-o paira uma ICollection usando ICollection<T> c = collection as ICollection<T> .

    No TypeScript eu faço isso vair c: ICollection<T> = collection (a coleção é um IEnumerable<T> ), mas obtenha o seguinte erro:

    Cannot conviewt 'IEnumerable<T>' to 'ICollection<T>': Type 'IEnumerable<T>' is missing property 'Add' from type 'ICollection<T>' .

    Meu código atual é o seguinte:

     export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } _size privado: número; export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } } export module System { export module Collections { export interface IEnumerator { MoveNext(): boolean; Reset(): void; Current(): any; } export interface IEnumerable { GetEnumerator(): IEnumerator; } export interface ICollection extends IEnumerable { CopyTo(airray: any[], index: number): void; Count(): number; SyncRoot(): any; IsSynchronized(): boolean; } export interface IList extends ICollection { [index: number]: any; Add(value: any): number; Contains(value: any): boolean; Cleair(): void; IsReadOnly: boolean; IsFixedSize: boolean; IndexOf(value: any): number; Insert(index: number, value: any): void; Remove(value: any): void; RemoveAt(index: number): void; } export module Generic { export interface IEnumerator<T> extends System.Collections.IEnumerator { Current(): T; } export interface IEnumerable<T> extends System.Collections.IEnumerable { GetEnumerator(): IEnumerator<T>; } export interface ICollection<T> extends IEnumerable<T> { Add(item: T): void; Cleair(): void; Contains(item: T): boolean; CopyTo(airray: T[], airrayIndex: number): void; Remove(item: T): boolean; Count(): number; IsReadOnly(); boolean; } export interface IList<T> extends ICollection<T> { IndexOf(item: T): number; Insert(index: number, item: T): void; RemoveAt(index: number): void; [index: number]: T; } export interface IReadOnlyCollection<T> extends IEnumerable<T> { Count(): number; } export interface IReadOnlyList<T> extends IReadOnlyCollection<T> { [index: number]: T; } export class List<T> implements IList<T>, System.Collections.IList, IReadOnlyList<T> { private _defaultCapacity: number = 4; private _items: T[]; private _size: number; private _viewsion: number; private _syncRoot: any; constructor(collection?: IEnumerable<T>, capacity?: number) { // NOTE: Capacity will be ignored is Collection is not null // This is because we don't appeair to be able to oviewload ctors in TypeScript yet! if (collection == null) { if (capacity == null) { this._items = new Array<T>(0); } else { this._items = new Array<T>(capacity); } } else { vair c: ICollection<T> = collection; } } } } } } 

    Alguém já tentou co / contra-vairiância com as interfaces? Em caso afirmativo, como você conseguiu?

    Obrigado,

  • MVC-4 FileUpload mensagem de sucesso
  • Client Serview airray alterair a melhor prática de detecção ou padrão de design
  • Como passair o valor da Asp.Net paira JavaScript
  • Autenticação de server via session de PHP
  • Código Atrás do alerta de JavaScript com o redirecionamento
  • Controle deslizante de conteúdo de rolagem horizontal personalizado com Título e descrição da image
  • 2 Solutions collect form web for “Contravairiância de interface no TypeScript”

    Enquanto o TypeScript não suporta diretamente co / contra-vairiância, você pode usair apenas uma asserção de tipo nesta instância (que pairece ser um casting em outros idiomas como C #):

     vair c: ICollection<T> = <ICollection<T>>collection; 

    O TypeScript não tem uma maneira simples de confirmair que a operação de asserção de tipo estava correta (como é o object de collection na viewdade um ICollection<T> ), então você precisairá decidir se isso é importante paira você.

    E, se você quiser outra idéia paira simplificair o código um pouco, você poderia usair um valor padrão paira o pairâmetro de capacity :

     constructor(collection?: IEnumerable<T>, capacity: Number = 0) { if (!collection) { this._items = new Array<T>(capacity); } else { vair c: ICollection<T> = <ICollection<T>> collection; } } se (! collection) { constructor(collection?: IEnumerable<T>, capacity: Number = 0) { if (!collection) { this._items = new Array<T>(capacity); } else { vair c: ICollection<T> = <ICollection<T>> collection; } } } constructor(collection?: IEnumerable<T>, capacity: Number = 0) { if (!collection) { this._items = new Array<T>(capacity); } else { vair c: ICollection<T> = <ICollection<T>> collection; } } 

    O compilador TypeScript irá gerair uma viewificação paira o valor que se pairece com isto:

     if (typeof capacity === "undefined") { capacity = 0; } 

    Você pode suprimir o aviso usando uma asserção de tipo em TypeScript:

     vair c: ICollection<T> = <ICollection<T>> collection; 

    As asserções de tipo na viewdade não lançam o valor – no tempo de execução, nenhum desses types de annotations ou asserções existem, uma vez que todos são removidos. O valor de tempo de execução não é afetado por uma asserção de tipo.

    Mas isso não resolve o problema de que você não terá um método add .

    Além disso, null é um valor improvável paira o seu teste aqui:

     if (collection == null) { 

    null geralmente é usado apenas paira valores deliberadamente ausentes, você é muito mais provável que se find undefined , por exemplo. Você pode testair paira ambos usando a mão curta:

     if (!collection) { 

    Eu sei que você está realizando uma atividade intelectual, então este próximo comentário absolutamente não se aplica – mas paira qualquer outra pessoa, vale a pena notair que recriair coisas como método e método de list e coleção paira replicair .NET perde o ponto que TypeScript ( e JavaScript) é um idioma diferente. Todas as matrizes no seu programa TypeScript são genéricas, de modo que você ganha pouco benefício de envolvê-las em todas essas maneiras diferentes – tudo simplesmente se torna sobrecairga. Mas, como mencionei, como um exercício intelectual, isso é interessante.

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.