Exclude or Remove method get TypeScript

typescript omit
typescript conditional types
typescript infer
typescript typeof
typescript record get keys
typescript generics
typescript interface
typescript never

I would like exclude or remove the method get in my class, example

export class User {

  password: string;

  public setPassword(password: string): void {
   this.password= password;

  // exclude or remove
  public getPassword(): string {
   return this.password;

I would don't want to send this data to firebase Database

A general way to shed methods from an instance is to use the JSON parser, which will result in just the properties remaining...

const user = new User();
user.password = 'test';

const propsOnly = JSON.parse(JSON.stringify(user));

This results in an object with a password property and no methods - and the JSON parser is more robust that something we might write ourselves. This trick is also robust to future methods that are added to the User class.

If you are sending JSON data to the API, you don't need to do this as the serialization will remove the methods anyway.

Utility Types · TypeScript, interface Todo { title: string; } const todo: Readonly<Todo> = { title: 'Delete Constructs a type by excluding from T all properties that are assignable to U . In the example above, the methods object in the argument to makeObject has a  To tell TypeScript how our generic rest types are structured we need to remove all ExtractName properties (name in this specific case). This is what Pick<Props, Exclude<keyof Props, keyof ExtractName>> does. Let's break this further down, to be getter a better understanding. Exclude removes specific keys:

One way to keep getPassword only to yourself would be to make its reference known only to you.

const GET_PASSWORD = Symbol();

export class User {
  private password: string;

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

  public setPassword(password: string): void {
   this.password= password;

  public [GET_PASSWORD](): string {
   return this.password;

const password = new User('foo')[GET_PASSWORD]()

Only the ones having access to GET_PASSWORD symbol will be able to call the method.

Although it's not the most elegant approach, and it probably makes sense to wrap your class into another one — this time without a getter.

TypeScript 2.8 · TypeScript, type Diff<T, U> = T extends U ? never : T; // Remove types from T that are assignable to U Exclude<T, U> – Exclude from T those types that are assignable to U  Utility Types Introduction # TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally. Table of contents # Partial<T> Readonly<T> Record<K,T> Pick<T,K> Omit<T,K> Exclude<T,U> Extract<T,U> NonNullable<T> Parameters<T> ConstructorParameters<T> ReturnType<T> InstanceType<T>

I am not familiar with firebase, but I suspect your concern is false. Even if not, removing a method would hardly help to remove it.

But if you really want to remove a method, then it is done this way:

let user = new User()
delete user.getPassword

If you however want to remove password from an object instance, then you should do something like this instead:

let user = new User()
user.password = ''

"Pick" or "Exclude" constructor from class type · Issue #29261 , "Pick" or "Exclude" constructor from class type #29261 This wouldn't be a breaking change in existing TypeScript/JavaScript code You can get at the constructor itself by using typeof ClassName , but Allow to add a call signature to the Mapped Type OR to remove all `Function.prototype` methods #  TypeScript 2.8 adds several predefined conditional types to lib.d.ts: Exclude<T, U> – Exclude from T those types that are assignable to U. Extract<T, U> – Extract from T those types that are assignable to U. NonNullable<T> – Exclude null and undefined from T. ReturnType<T> – Obtain the return type of a function type.

Notes on TypeScript: Pick, Exclude and Higher Order Components , Let's take a look at the following example to get a better idea: Date; }; Exclude<​User, "id" | "registeredAt"> // removes id and registeredAt. To actually start using the workspace version for IntelliSense, you must run the TypeScript: Select TypeScript Version command and select the workspace version. Using TypeScript nightly builds. The simplest way to try out the latest TypeScript features in VS Code is to install the JavaScript and TypeScript Nightly extension.

TypeScript 3.5 Adds Omit Type, Smarter Union Type Checking, The TypeScript team has announced the release of TypeScript 3.5, including type checking performance improvements and a new Omit type. TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control over how a member is accessed on each object. Let’s convert a simple class to use get and set .

Conditional Types in TypeScript, TypeScript 2.8 introduced conditional types, a powerful and exciting addition Exclude null and undefined from T */ type NonNullable<T> = T It effectively allows us to remove constituent types from a union type. the NonNullablePropertyKeys<T> type to find out which properties are isArray() methods. TypeScript 2.8 adds the ability for a mapped type to either add or remove a particular modifier. Specifically, a readonly or ? property modifier in a mapped type can now be prefixed with either + or - to indicate that the modifier should be added or removed.

  • but the way I'm to making I use the method : user = new User() ..... this.user.setPassword(value)
  • I'm record data in firebase in authentication and database, but I dont want to send the password to database .....
  • You can't hide property this way, because there is Object.getOwnPropertySymbols()
  • You still need to have the symbol to dereference their values.
  • Aand you can find it with the Object method defined above. I mean symbols make it harder, but not impossible.
  • Would you be willing to provide an example on how to obtain the password without having GET_PASSWORD?
  • I hope you can understand it yourself after having a look at this but if not, ask me and I'll provide the code.