I was initially under the impression that TypeScript would throw an error due to passing the incorrect number of elements in EntryPoints
, but surprisingly, no error occurred.
function createContext<T>(defaultValue: T): T[] {
return [defaultValue]
}
interface EntryPoints {
parentSelector: string;
}
interface SomeType {
entryPoints: EntryPoints[];
}
const defaultData = {
entryPoints: [{
parentSelector: '',
foo: 1 // <-- error expected here
}]
}
createContext<SomeType>(defaultData)
The same code without generics behaves as anticipated
function createContext<T>(defaultValue: T): T[] {
return [defaultValue]
}
interface EntryPoints {
parentSelector: string;
}
interface SomeType {
entryPoints: EntryPoints[];
}
const defaultData: SomeType = {
entryPoints: [{
parentSelector: '',
foo: 1 // <-- error thrown here
}]
}
createContext(defaultData)