Update: Starting with KOMA-Script version 3.27 there is also option pagenumberwidth
for \DeclareTOCStyleEntry
to adjust the space for the page number. With prefix toc
it can be used for \RecdeclareSectionCommand
too.
\documentclass{scrbook}[2019/10/12]
\RedeclareSectionCommands[tocdynnumwidth]{chapter,section}
\RedeclareSectionCommands[tocpagenumberwidth=6ex]% adjust the space for the page number
{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}
\begin{document}
\tableofcontents
\setcounter{chapter}{100}% just for the example
\setcounter{page}{998}% just for the example
\chapter{Title of chapter}
\setcounter{section}{10}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\chapter{Title of chapter}
\setcounter{section}{20}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\end{document}
Starting with KOMA-Script version 3.20 you can use
\documentclass{scrbook}[2016/10/05]
\RedeclareSectionCommands[tocdynnumwidth]{chapter,section}
\RedeclareSectionCommands[tocpagenumberbox=\pagenumberbox]
{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}
\newcommand\pagenumberbox[1]{%
\makebox{\enskip #1}%
}
\begin{document}
\tableofcontents
\setcounter{chapter}{100}% just for the example
\setcounter{page}{998}% just for the example
\chapter{Title of chapter}
\setcounter{section}{10}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\chapter{Title of chapter}
\setcounter{section}{20}% just for the example
\section{Title of a Section}
\section{Title of a Section}
\end{document}

Or
\RedeclareSectionCommand[tocnumwidth=3em]{chapter}
\RedeclareSectionCommand[tocindent=3em,tocnumwidth=3.5em]{section}
\RedeclareSectionCommands[tocpagenumberbox=\pagenumberbox]
{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}
\newcommand\pagenumberbox[1]{%
\makebox[4ex][r]{#1}%
}
