Environment
The current version of TypeScript is 3.2.1 and the configuration file "tsconfig.json" looks like this:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
}
}
Question
I am interested in finding a type similar to "Partial" 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 both for your input. I have tested both types and they pass the compiler's check successfully!
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;