Encountering difficulties compiling the code below. This snippet is part of an older project with the note - "This is a TS1.8 feature. Leave it in to ensure the environment is running the right ts".
function assign<T extends U, U>(target: T, source: U): T {
for (let id in source) {
target[id] = source[id]; // error TS2322: Type 'U[Extract<keyof U, string>]' is not assignable to type 'T[Extract<keyof U, string>]'.
}
return target;
}
Trying to compile it with this command
tsc -p tsconfig.json
And using this tsconfig.json file
{
"include": [
"Scripts/TypeScripts/**/*"
],
"compilerOptions": {
"target": "es5",
"module": "amd",
"sourceMap": false,
"watch": false
}
}
tsc -v
shows Version 3.4.5
.
Testing it on the playground, I also encounter the same error, raising doubts about the validity of the code. This leads me to question why I wrote that comment and how it managed to compile for 2 years (if it did).
Hence my query: Is this valid TypeScript code? If not, was it ever?
Thank you :-)