7 Matching Annotations
- Oct 2020
-
xtzmf.csb.app xtzmf.csb.app
- Oct 2019
-
stackoverflow.com stackoverflow.com
-
type KeysOfType<A extends object, B extends { [key: string]: any }> = { [K in keyof A]: A[K] extends B ? string extends keyof A[K] ? K : never : never; }[keyof A];
-
-
github.com github.com
-
type FindByTag<Union, Tag> = Union extends { tag: Tag } ? Union : never; function cast<A extends Foo["tag"]>(foo: Foo, expectedTag: A): FindByTag<Foo, A> { if (foo.tag !== expectedTag) throw Error(`expected tag ${expectedTag} but was ${foo.tag}`) return foo as FindByTag<Foo, A>; }
-
-
stackoverflow.com stackoverflow.com
-
type TagWithKey<TagName extends string, T> = { [K in keyof T]: { [_ in TagName]: K } & T[K] };
-
-
www.typescriptlang.org www.typescriptlang.org
-
type ReturnType<T extends AnyFunction> = T extends (...args: any[]) => infer R ? R : any;
-
-
github.com github.com
-
type Type = 'a' | 'b'; type AShape = { a: 'a' }; type BShape = { b: 'b' }; type Props<T extends Type> = { type: T, shape: T extends 'a' ? AShape : BShape, }; class Test<T extends ID> extends React.Component<Props<T>> { render() { const { type, shape } = this.props; switch (type) { case 'a': return <>{shape.a}</>; // Ideally would narrow `shape` here, instead of `AShape | BShape` default: return <>{shape.b}</>; } } } <T type="a" shape={{ a: 'a' }} /> // No error in ideal case <T type="a" shape={{ b: 'b' }} /> // error in ideal case
-
type NumberType = (() => number) | number; function double<T extends NumberType>( num: T ) : T { if (typeof num === "number") return num * 2; return () => num() * 2; }
-