Skip to main content

TS4111 - noPropertyAccessFromIndexSignature

· 2 min read
Simon Porritt
VisuallyJs Development
info

This post is from JsPlumb, which is now in maintenance mode. We've updated the Gantt chart link to point to VisuallyJs.

While working on an Angular version of our popular Gantt chart demonstration this morning I stumbled across a Typescript linting issue whose purpose is not at all clear to me.

THE PROBLEM​

Say I have this interface for a person:

export interface Person {    
firstName:string
lastName:string
}

and this for a serialized person:

export interface Person {    
firstName:string
lastName:string
fullName:string
}

Now I want to write a serializer to export a bunch of these people:

export function serializePeople(persons:Array<Person>):string {
return persons.map(p => {
return {
fullName:`${p.firstName} ${p.lastName}`,
firstName:p.firstName,
lastName:p.lastName
}
})
}

This looks ok, right? Well in fact the default settings for Angular's tsconfig.json cause an error:

Property 'firstName' comes from an index signature, so it 
must be accessed with ['firstName']

Huh? Is it not the case that if I were to switch to the suggested way to access the property I would lose type checking? This seems like nonsense to me. I could rewrite my serializer to this, yes:

export function serializePeople(persons:Array<Person>):string {
return persons.map(p => {
return {
fullName:`${p['firstName']} ${p['lastName']}`,
firstName:p.firstName,
lastName:p.lastName
}
})
}

...and I could also rewrite it to this, which would compile, but fail at runtime with a null pointer exception:

export function serializePeople(persons:Array<Person>):string {
return persons.map(p => {
return {
fullName:`${p['firstName']} ${p['lastName']}`,
firstName:p.firstName,
lastName:p.lastName,
lengthOfUnknownField:p['someUnknownField'].length
}
})
}

It isn't clear to me what the purpose of this linting rule is. The reason people move to Typescript is exactly to get away from situations like this.

THE FIX​

Fortunately the fix for this is straightforward - look for noPropertyAccessFromIndexSignature in your tsconfig.json and set it to false:

"compilerOptions": {
...
"noPropertyAccessFromIndexSignature":false
...
}


Try VisuallyJs​

VisuallyJs offers an extensive list of starter apps, diagrams, charts and dashboards to quick start your development, in React, Angular, Vue, Svelte and Typescript/Javascript.

VisuallyJs - industry standard diagramming and rich visual UI Javascript and Typescript library
Call Flow
Use VisuallyJs to build a visual Call Flow editor
VisuallyJs - fully featured alternative to ReactFlow and ngDiagram
AI Agent Builder
Use VisuallyJs to create an advanced AI agent builder
When you've reached the limits with ReactFlow, VisuallyJs has what you need
Chatbot
Use VisuallyJs to build a Chatbot editor, with actions, messages, input and choices
When you've reached the limits with SvelteFlow, VisuallyJs has what you need
Flowchart
Fully featured flowchart builder including support for custom shapes, edge routing to avoid vertices, shape resize/rotate, SVG/PNG/JPG export and more
When you've reached the limits with ngDiagram, VisuallyJs has what you need
BPMN
BPMN editor for modelling the steps of a business process. Pools, lanes, and a full set of task, event and gateway types
VisuallyJs - flowcharts, AI Agent builders, chatbots, bar charts, decision trees, mindmaps, org charts and more
ERD
ERD editor for modelling the steps of a business process
VisuallyJs - effortlessly build professional node based UIs with Javascript, Typescript, React, Svelte, Angular and Vue
Gantt
Interactive Gantt chart featuring tasks, task groups and milestones
VisuallyJs - build diagrams and rich visual UIs fast
Kanban
Fully featured Kanban board. Drag items between columns and use the inspector to update items and columns
VisuallyJs - fully featured alternative to ReactFlow and ngDiagram
Database Schema
Database Schema builder with support for tables, views, multiple columns types, and column relationships
VisuallyJs - build diagrams and rich visual UIs fast
Org Chart
Uses the classic org chart layout and provides an inspector from which the user can navigate around
VisuallyJs - build diagrams and rich visual UIs fast
Circuit Diagram
Fully featured starter app containing a circuit diagram builder
When you've reached the limits with ReactFlow, VisuallyJs has what you need
Scada/HMI
Professional and modern Scada/HMI application with fluid, heating/cooling & instrumentation shapes, adhering to the HMI ISA-101 Design Standard
When you've reached the limits with ngDiagram, VisuallyJs has what you need
Mindmap
The mindmap builder highlights several advanced features, such as custom layouts, parsers and exporters
When you've reached the limits with ngDiagram, VisuallyJs has what you need
Neighbourhood Views
Demonstrates how to include multiple views of a dataset on one page
VisuallyJs - callflow builders, sankey charts, database schemas, ERD diagrams and more
Logic Gates
Fully featured starter app containing a logic gates diagram builder
VisuallyJs - effortlessly build professional node based UIs with Javascript, Typescript, React, Svelte, Angular and Vue
Template
Basic starter app demonstrating how to setup VisuallyJs and its main features
VisuallyJs - build diagrams and rich visual UIs fast
Area & Line charts
Use VisuallyJs to create area and line charts
VisuallyJs - industry standard diagramming and rich visual UI Javascript and Typescript library
Bar & Column charts
Multiple series, stacked, grouped, pivoted, and much more
VisuallyJs - callflow builders, sankey charts, database schemas, ERD diagrams and more
Scatter & Bubble charts
Circle, rectangle, triangle or custom markers, multiple series, fully customizable
VisuallyJs - flowcharts, AI Agent builders, chatbots, bar charts, decision trees, mindmaps, org charts and more
Sankey chart
Use VisuallyJs to create a professional Sankey chart, with support for pivoting
When you've reached the limits with ngDiagram, VisuallyJs has what you need
Supply Chain Analyzer
Dashboard for managing and analyzing supply chains
VisuallyJs - fully featured alternative to ReactFlow and ngDiagram
Network Infrastructure
Combine a network management diagram with charts showing projected cost and resource usage
VisuallyJs - callflow builders, sankey charts, database schemas, ERD diagrams and more
Scrolling Lists
Use the ListManager plugin to manage scrolling lists: as elements are scrolled out of the view, their edges are moved to the list container
VisuallyJs - build diagrams and rich visual UIs fast
FIFA World Cup
A visualizer for the FIFA World cup - group stages, team journeys and a tournament view.
When you've reached the limits with SvelteFlow, VisuallyJs has what you need
Fault Tree Analysis
Combines a fault tree analysis diagram with charts showing risk and list of cut sets