Overriding interface property type defined in Typescript d.ts file
I use a method that first filters the fields and then combines them.
reference Exclude property from type
interface A {
x: string
}
export type B = Omit<A, 'x'> & { x: number };
for interface:interface A {
x: string
}
interface B extends Omit<A, 'x'> {
x: number
}
How to override type properties in TypeScript
Create a helper type:
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
Usage:type HexColorLine = Overwrite<Line, { color: number }>
Overriding interface property type from exported type
If I understood your question correctly, I think this is the type you want:
You don't need to supply generic type parameters when you are choosing the same ones as the defaults.TS Playground
interface Place extends Omit<Collections.Places.Entity, 'assignedPlaces'> {
assignedPlaces?: NewPlaces[];
bounds: Bounds;
center: Position;
id: string;
}
declare const place: Place;
place.assignedPlaces; // NewPlaces[] | undefined
Override the properties of an interface in TypeScript
UPDATE, 2018-08
TypeScript 2.8 introducedExclude<T, U>
which behaves like the Diff<T, U>
defined below for all types (not just key types). You should definitely use Exclude<>
instead of Diff<>
if you are using TypeScript 2.8 or above.Also, in TypeScript 2.9, keyof any
was expanded from string
to string | number | symbol
, so the below Diff<T, U>
caused errors which can be fixed by changing Diff<T extends string, U extends string>
to Diff<T extends keyof any, U extends keyof any>
. This change has been made below.
ORIGINAL ANSWER, 2018-03
Yes, conditional types will enable this, although you can get this behavior without them as well.The idea is to pull properties out of the original interface and then replace them with new ones. Like this:
type Diff<T extends keyof any, U extends keyof any> =
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
type Overwrite<T, U> = Pick<T, Diff<keyof T, keyof U>> & U;
interface OriginalInterface {
title?: string;
text?: string;
anotherProperty?: SomeType;
// lots of other properties
}
interface Extension {
title?: string | ReactElement<any>;
text?: string | ReactElement<any>;
}
interface ExtendedInterface extends Overwrite<OriginalInterface, Extension> {};
const ext: ExtendedInterface = {}; // okay, no required properties
ext.text; // string | ReactElement<any> | undefined
ext.title; // string | ReactElement<any> | undefined
ext.anotherProperty; // SomeType | undefined
EDIT: I changed the definition of Overwrite<T,U>
to respect the optional/required status of properties from T
whose keys are not present in U
.This has the behavior you want, I think. Hope that helps; good luck!
Access dynamic property of an interface in Typescript
type X = Person[keyof Person];
Will return type:type X = {
name: string;
alias: string;
active: string[];
values: string[];
}
Related Topics
How to Call a Dynamically-Named Method in JavaScript
Cross Domain Localstorage with JavaScript
JavaScript String Array to Object
How to Get Size of an Element/List in Firebase Without Get It All
Differencebetween Settimeout(Fn, 0) and Settimeout(Fn, 1)
Regular Expression to Match Exactly 5 Digits
JavaScript Property with Three Dots (...)
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways
What Is the 'Execution Context' in JavaScript Exactly
How Do Cors and Access-Control-Allow-Headers Work
Detect Whether Scroll Event Was Created by User
How to Load a JSON Object from a File with Ajax
How to Find a Reason Angularjs "Argument 'Myctrl' Is Not a Function, Got Undefined"
Basic Authentication with Fetch