Before going further, let me show you an example in JavaScript:
let a = 5
function fn() {
console.log(a)
}
fn() // 5
a = 10
fn() // 10
The first function call logs the output 5, and the last call logs to 10.
In this sense, what I am thinking of TypeScript's interface behavior to merge. An example would be good to illustrate this:
Interfaces are merged:
interface Example {
foo: string
}
interface Example {
bar: string
}
So, this becomes:
interface Example {
foo: string
bar: string
}
Now, let me show you an example representing the thoughts:
interface Person {
name: string
}
function myPersonFn() {
interface Person {
age: number
}
const inPerson: Person = {name: 'Bhojendra', age: 37}
console.log(inPerson)
}
interface Person {
address: string
}
const outPerson: Person = {name: 'Rauniyar', address: 'Kathmandu'}
console.log(outPerson)
This throws an error: (Good, the interface Person is function scoped.)
Type '{ name: string; age: number; }' is not assignable to type 'Person'.
- Object literal may only specify known properties, and 'name' does not exist in type 'Person'.
Now, let's try extending it:
interface Person {
name: string
}
function myPersonFn() {
// type ScopedPerson = Person & {
// age: number
// }
interface ScopedPerson extends Person {
age: number
}
const inPerson: ScopedPerson = {name: 'Bhojendra', age: 37}
console.log(inPerson)
}
myPersonFn()
interface Person {
address: string
}
const outPerson: Person = {name: 'Rauniyar', address: 'Kathmandu'}
console.log(outPerson)
This throws an error:
Property 'address' is missing in type '{ name: string; age: number; }' but required in type 'ScopedPerson'.
This is in fact typescript behavior. As it merges the interfaces the Person interface expects address to be in it.
But I could ask to TypeScript, can I ignore that?
Well, what if the function call at an end?
interface Person {
address: string
}
myPersonFn()
Hmm, this makes us think that TypeScript is doing best thing for us not allowing to miss the address property.
Wait! What I am thinking for this is behave like JavaScript code as the a value logged in first code block. Mmm, generic something like to meet both behavior?
interface ScopedPerson<T, Optional> extends Person {
I don't know if this is even possible? You might have an idea, if you get my point?
What I want is, do not throw error, let it compile OK this line of code inside the function block:
const inPerson: ScopedPerson = {name: 'Bhojendra', age: 37}
console.log(inPerson)
Pretty well, I am not talking about optional address property:
interface Person {
address?: string
}