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