Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Partially "Partial" type

Tags:

typescript

Environment

TypeScript's version is 3.2.1 and "tsconfig.json" is like below.

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true
  }
}

Question

I'm looking for Partially "Partial" type in TypeScript.

type Entity = {
  a: string,
  b: string,
  c?: string,
};

type Ham = MyType<Entity, 'b'>;
/**
 * expected to equal
 * {
 *   a: string,
 *   b?: string, // changed to be optional
 *   c?: string,
 * };
 */

P.S. Titian and t7yang

Thank you for your replies. I checked your types then both types pass compiler's check!

const abc = { a: 'a', b: 'b', c: 'c' };
const ab = { a: 'a', b: 'b' };
const ac = { a: 'a', c: 'c' };
const a = { a: 'a' };

// by t7yang
let test1Abc: OptionalKey<Entity, 'b'> = abc;
let test1Ab: OptionalKey<Entity, 'b'> = ab;
let test1Ac: OptionalKey<Entity, 'b'> = ac;
let test1A: OptionalKey<Entity, 'b'> = a;

// by Titian Cernicova-Dragomir    
let test2Abc: PickPartial<Entity, 'b'> = abc;
let test2Ab: PickPartial<Entity, 'b'> = ab;
let test2Ac: PickPartial<Entity, 'b'> = ac;
let test2A: PickPartial<Entity, 'b'> = a;
like image 422
edhiwo Avatar asked Dec 12 '18 11:12

edhiwo


People also ask

What is partial type in TypeScript?

The Partial type in TypeScript is a utility type which does the opposite of Required. It sets all properties in a type to optional.

What does ?: Mean in TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

How does partial work in TypeScript?

The partial utility type was introduced in TypeScript release 2.1 and it is designed to make all of the properties of a type optional. This means developers will no longer have to provide values to all properties of a type. In fact, it opens the possibility of not providing any property.

How do I create a partial class in TypeScript?

Partial classes for TypeScript Unfortunately, TypeScript doesn't support partial classes. And according to this thread, it will never support it.


2 Answers

You can use Pick in conjunction with Partial to pick only the properties you want to make optional, while preserving the rest using Exclude to get the keys excluding the ones passed in to make optional :

type Entity = {
   a: string,
   b: string,
   c?: string,
};

type PickPartial<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & Partial<Pick<T, K>> 
type Ham = PickPartial<Entity, 'b'>; // a, b? , c?
like image 181
Titian Cernicova-Dragomir Avatar answered Sep 20 '22 16:09

Titian Cernicova-Dragomir


type Entity = {
  a: string,
  b: string,
  c?: string,
};

type OptionalKey<T, O extends keyof T> = Pick<T, Exclude<keyof T, O>> & Partial<{ [P in O]: T[P] }>;

const a: OptionalKey<Entity, 'b'> = {
  a: 'a',
}

const ab: OptionalKey<Entity, 'b'> = {
  a: 'a',
  b: 'b'
}

const ac: OptionalKey<Entity, 'b'> = {
  a: 'a',
  c: 'c'
}

The idea is pick all the properties that want to make optional, then merge with the type than we want to make the property optional.

You can check this in typescript playground

like image 27
t7yang Avatar answered Sep 20 '22 16:09

t7yang