Typescript get type that excludes all functions from a class

typescript exclude
typescript keyof
typescript typeof
typescript infer
typescript mapped types
typescript conditional types
typescript types
typescript partial class

I copy the data from a class instance using the spread operator: const myObj:MyType = {...myClassInstance}, where myClassInstance is an instance of MyClass. I want to define that type MyType, which excludes all functions defined on MyClass, keeping only strings, numbers, arrays etc. I tried this:

type ExcludeSubType<Base, Condition> = Pick<Base, {
  [Key in keyof Base]: Base[Key] extends Condition ? never : Key
}[keyof Base]>;

export type MyType = ExcludeSubType<MyClass, (_: any) => any>;

MyClass might look something like:

export class MyClass {
  foo: string;

  constructor(foo: string) {
    this.foo = foo;

  stopTimer() {
    // stuff

  startTimer() {
    // stuff

  continue(p1: string, p2: number) {
    // stuff

  setFailed(p1: string, p2: string) {
    // more stuff

But MyType still has functions on it - in particular the functions with arguments. What am I doing wrong?

export type MyType = ExcludeSubType<MyClass, Function>;

The type (_: any) => any only matches functions that take at most one argument.

Handbook - Utility Types, interface Todo { title : string; description : string; } function updateTodo ( todo : Todo Constructs a type by excluding from Type all union members that are class C { x = 0; y = 0; } type T0 = InstanceType <typeof C >; // ^ = type T0 = C type T1� TypeScript comes with a ton of built in generic types that ease your development workflow. Here’s a list of all built-in generic types, with examples! Note : This list is probably (most definitely) not complete.

You need to make _ a rest parameter in order to match functions of any parameter arity:

export type MyType = ExcludeSubType<MyClass, (..._: any) => any>;

Playground Link

TypeScript 2.8 � TypeScript, Then, given a type T'' that is an instantiation of T where all infer type variables are Exclude null and undefined from T . ReturnType<T> – Obtain the return type of a InstanceType<T> – Obtain the instance type of a constructor function type. string[] function f1(s: string) { return { a: 1, b: s }; } class C { x = 0; y = 0; } type T10� Correct me if I'm wrong but it looks like @logProperty decorator defines property on prototype making things a bit wrong: as I can see running code from typescript playground this is a window object and then once you make several objects of type Person, changing the name for one of them will resolve in changing the name for all the others (since this property lives in prototype)?

"Pick" or "Exclude" constructor from class type � Issue #29261 , Search Terms intersect, type, intersect constructor, pick new, pick error new a('') .baz(); // error type T2 = typeof C1 & Exclude<typeof C2, new> const This wouldn't be a breaking change in existing TypeScript/JavaScript code to the Mapped Type OR to remove all `Function.prototype` methods #27575. Interfaces are not to be converted to JavaScript. It’s just part of TypeScript. If you see the screen shot of TS Playground tool there is no java script emitted when you declare an interface unlike a class. So interfaces have zero runtime JavaScript impact. Union Type and Interface. The following example shows the use of Union Type and

Conditional types in TypeScript, Let's add basic types to this function so we can let TypeScript worry about typeof process("foo") // => string typeof process(null) // => null typeof interface Shape { color: string } class Circle { color: string radius: number } // ✓ All good! Exclude from U those types that are assignable to T type Exclude<U,� Index Type Query and Utility Types (Omit, Pick, Exclude and Partial) First of all the index type query or keyof enumerates the properties of a type. Using the standard TypeScript documentation

typescript-cheatsheet, The cheatsheet contains references to types, classes, decorators, and many other TypeScript In TypeScript, all newly declared object properties (including both function Before we get into what an intersection type is, I must mention that in the In TypeScript 2.8, the Exclude type was added to the standard library , which� % Subtyping and type compatibility: program first must pass all the TypeScript checks (to catch errors); we then perform subtyping and type compatibility checks again. % Syntactically, many ways to describe object types. STS % - Class type reference % - Interface type reference, % - array types, tuple types, function types, and constructor types

TypeScript: Built-in generic types, More on TypeScript Here's a list of all built-in generic types, with examples! Readonly; ReadonlyArray; ReturnType; Partial; Required; NonNullable; Pick; Record; Extract; Exclude; Omit; Bottom line type Square = ReturnType<typeof generateSquare>; You can also access functions inside classes: If functions in the array don’t share the same parameter type, the inferred type should be never. This is based on TypeScript’s generic type inference behavior upon contro-variant type variables.

  • Could you share an example of MyClass definition? I think I understand what's wrong, but don't want to accidentally bark on a wrong tree.
  • The type appears to exclude functions.. are you looking for a runtime effect ? typescriptlang.org/play/…
  • @TitianCernicova-Dragomir I want it to exclude functions. It does not (well, not all of them).
  • @Cerberus - added, thanks.
  • @seesharper Did not have my coffee yet missed multi parameter 😅. You need a ... export type MyType = ExcludeSubType<MyClass, (..._: any) => any>;