I\'m working on converting my little library from JavaScript to TypeScript, and I have a function there
function create(declarations: Declarations) {
It seems this is not possible to do, in the TypeScript Deep Dive book there's a section on exactly this. Although you can declare a type you can't actually create an object with it in TS:
// No error on this type declaration:
type Declarations = {
[key: string]: string;
} & {
onMember: number;
onCollection: number;
}
// Error does appear here indicating type `number` is not assignable to type `string`.
const declarations: Declarations = {
onMember: 0,
onCollection: 0,
other: 'Is a string'
}
TypeScript Playground link.