Typescript Changing the definition of some third party types d ts

0 votes

I am a little bit lost how to do the following.

I am trying to change the definitions of some third party types by creating a new file thirdParty.d.ts.

Let's suppose the third party returns Class A

class A {
    // ...
}

In my third-party.d.ts file, I want to introduce two new parameters.

import * as thirdParty from 'thirdParty'
decalre module 'thirdParty' {
    export interface A extends thirdParty.A {
        newParam1: number
        newParam2: number
    }
}

Then, let's overwrite class A by adding newParam1 and newParam2

class ExtendedA extends A {
    newParam1 = 1
    newParam2 = 2
}

So now everything looks good. Every instance of class A recognizes the new parameters. In any function or class it's possible to call newParam1 without casting.

randomMethod() {
 console.log(this.a.newParam1) // Returns 1. No need to cast (this.a as ExtendedA).newParam1 !
}

However, since I changed the definition of class A. And ExtendedA extends it. Deleting the new parameters will not generate errors. Which worries me. I am looking for a way to force ExtendedA to decalre the new parameters.

// This is bad :(    
class ExtendedA extends A { // implements Interface will not work either
    // newParam1 = 1 // Commented but there is no errors ! Which is bad
    // newParam2 = 2
}

I am sure the fix is pretty easy but I am really lost.

Jul 18 in TypeSript by Logan
• 2,140 points
10 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer
0 votes
0 answers
0 votes
0 answers

In typescript, why do the | and & operators flip their meaning when used on function types?

In this code, example1 and example2 are confusing me: type F1 = ...READ MORE

Jul 13 in TypeSript by Logan
• 2,140 points
25 views
0 votes
0 answers

TypeScript: Typing the result of an external module's non-mutating extension function

Let's say I'm using external package "foo". ...READ MORE

Jul 18 in TypeSript by Logan
• 2,140 points
14 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,200 points
511 views
0 votes
1 answer

Can't bind to 'formGroup' since it isn't a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10 in Others by Rahul
• 9,000 points
1,257 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
722 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP