See the update for referring to unnumbered/numbered structure unit and checking at the end.
Without using any extra packages, only \renewcommand, \let, \pdfstrcmp etc. are used and writing a \@namedef to the .aux file.
It requires two runs (which are needed anyway, since we're dealing with labels!)
Use \extractlabeltype{labelname} and \checklabeltype with true/false branch.
It assumes that no other package is involving with \label and \ref, i.e. no hyperref or cleveref is supported.
\documentclass{book}
\makeatletter
\let\latex@@refstepcounter\refstepcounter
\let\latex@@label\label%
\renewcommand{\refstepcounter}[1]{%
\gdef\lastrefsteppedcounter{#1}%
\latex@@refstepcounter{#1}%
}
\renewcommand{\label}[1]{%
\immediate\write\@auxout{\string\global\string\@namedef{label#1}{\lastrefsteppedcounter}}
\latex@@label{#1}%
}
\newcommand{\extractlabeltype}[1]{%
\@nameuse{label#1}%
}
\makeatother
\newcommand{\checklabeltype}[4]{%
\ifnum0=\pdfstrcmp{\extractlabeltype{#1}}{#2}
#3%
\else
#4%
\fi
}
\begin{document}
\chapter{Foo} \label{foo}
\section{Foosection}\label{foosection}
\checklabeltype{foosection}{section}{Yes, it is section}{No, it is something different}
\checklabeltype{foo}{section}{Yes, it is section}{No, it is something different}
\end{document}

Update with checking for unnumbered/numbered issues.
Please not that this deliberately depends on the assumption that the anchor name of a unnumbered structure unit has a * in it, so playing around with \theH... macros is no good idea ;-)
\documentclass{book}
\usepackage{xparse}
\usepackage[hyperref,counter]{zref}% Using the counter mechanism behind `nameref`
\usepackage{hyperref}
\makeatletter
\AtBeginDocument{%
\let\latex@@label\label%
\renewcommand{\label}[1]{%
\zref@label{#1}%
\latex@@label{#1}%
}
% Get the underlying counter type
\newcommand{\extractlabelcounter}[1]{%
\zref@ifrefundefined{#1}{%
???????}{%
\zref@extract{#1}{counter}%
}%
}
% Get the anchor name for hyperref or nameref -> has a `*` inside if it is unnumbered
\newcommand{\extractlabelanchor}[1]{%
\zref@ifrefundefined{#1}{%
???????}{%
\zref@extract{#1}{anchor}%
}%
}
}
% Check if there's a `*` inside of the anchor name
\ExplSyntaxOn
\cs_new:Npn \checkifnumbered#1#2#3{%
\tl_set:Nx \l_tmpa_tl {\extractlabelanchor{#1}}
\tl_if_in:NnTF \l_tmpa_tl {*} {#2} {#3}
}
\ExplSyntaxOff
\makeatother
\newcommand{\checklabeltype}[4]{%
\ifnum0=\pdfstrcmp{\extractlabelcounter{#1}}{#2}
#3%
\else
#4%
\fi
}
\begin{document}
\chapter{Foo} \label{foo}
\section*{An unnumbered section} \label{unnumbered}
\section{Foosection}\label{foosection}
\checklabeltype{foosection}{section}{Yes, it is section}{No, it is something different}
\checklabeltype{foo}{section}{Yes, it is section}{No, it is something different}
\begin{enumerate}
\item First \label{enumfirst}
\item Second \label{enumsecond}
\end{enumerate}
\checklabeltype{enumsecond}{enumi}{It is a numbered item and has the value \ref{enumsecond}}{}
In \nameref{unnumbered} we have an \checkifnumbered{unnumbered}{unnumbered}{numbered} \extractlabelcounter{unnumbered}
In \nameref{foo} we have an \checkifnumbered{foo}{unnumbered}{numbered} \extractlabelcounter{foo} whereas
\nameref{foosection} is a \checkifnumbered{foosection}{unnumbered}{numbered} \extractlabelcounter{foosection}.
\end{document}

zrefis a good friend, see for example my answer to this question here: http://tex.stackexchange.com/questions/312060/how-to-reference-sections-in-other-parts-mentioning-the-part – Feb 01 '17 at 12:38cleverefas well – Feb 01 '17 at 12:44scrreportmakes use of in the final end? ;-) Using no extra packages, this you have to do redefinitions in the\label-\refsystem. This requires someLaTeXskills. I don't see the point to reinvent the wheel. – Feb 01 '17 at 12:47hyperref's\autorefcan be used for this. – Schweinebacke Feb 01 '17 at 12:51\autorefis just the wanted result. Maybe redefining the names may be enough. Maybe not. – Schweinebacke Feb 01 '17 at 12:54\labelmacro (and likely break all kinds of stuff in the process...). By default, the\labelinstruction looks for the counter variable that was most recently incremented via a\refstepcounterinstruction. However, if some item is not numbered to begin with, there is simply no such association to latch on to. (to be continued) – Mico Feb 01 '17 at 13:14\hyperlink-\hypertargetmachinery of thehyperrefpackage. But then, you seem to be averse to loading any external packages... – Mico Feb 01 '17 at 13:15hyperrefuses a*in the anchor name. As long as nobody screws up the\theH....outputzrefcan be used to exploit the anchor name and check, whether there is a*inside (and of course nobody should define a counter name with*inside) – Feb 03 '17 at 13:37\labelinstruction. Good to know thathyperrefmodifies the\labelmacro in interesting ways. – Mico Feb 03 '17 at 15:08